Attempting To Compile And Run The Java Program

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Venkatasanthosh
V
Venkatasanthosh
Community Contributor
Quizzes Created: 1 | Total Attempts: 580
Questions: 10 | Attempts: 580

SettingsSettingsSettings
Attempting To Compile And Run The Java Program - Quiz

.


Questions and Answers
  • 1. 

    class ColorException extends Exception {} class WhiteException extends ColorException {} class White { void m1() throws ColorException {throw new WhiteException();} void m2() throws WhiteException {} public static void main (String[] args) { White white = new White(); int a,b,d,f; a = b = d = f = 0; try {white.m1(); a++;} catch (ColorException e) {b++;} try {white.m2(); d++;} catch (WhiteException e) {f++;} System.out.print(a+","+b+","+d+","+f); }} What is the result of attempting to compile and run the program?

    • A.

      Prints: 0,1,0,0

    • B.

      Prints: 1,1,0,0

    • C.

      Prints: 0,1,1,0

    • D.

      Prints: 1,1,1,0

    Correct Answer
    C. Prints: 0,1,1,0
    Explanation
    The first try block contains two statements. The first invokes method m1, and the subsequent statement contains a post increment expression with the variable, a, as the operand. Method m1 throws a WhiteException exception, so variable a is not incremented as control passes to the catch block where b is incremented. The throws clause of m1 declares a ColorException, so the body may throw a ColorException or any subclass of ColorException. The second try block also contains two statements. The first invokes method m2, and the subsequent statement contains a post increment expression with the variable, d, as the operand. Method m2 does not throw an exception, so d is incremented, and the try block completes normally. Although the throws clause of m2 declares a WhiteException, there is no requirement to throw any exception.

    Rate this question:

  • 2. 

    interface A { void m1(); // 1 public void m2(); // 2 protected void m3(); // 3 private void m4(); // 4 } Compile-time errors are generated at which lines?

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer(s)
    C. 3
    D. 4
    Explanation
    Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error.

    Rate this question:

  • 3. 

    Which of the following are modifiers that can be applied to an interface that is a member of a directly enclosing interface?

    • A.

      Abstract

    • B.

      Implements

    • C.

      Final

    • D.

      Private

    • E.

      Protected

    • F.

      Public

    Correct Answer(s)
    A. Abstract
    F. Public
    Explanation
    All interfaces are implicitly abstract. The explicit application of the abstract modifier to an interface declaration is redundant and is strongly discouraged. The declaration of an interface within the body of an enclosing class or interface is called a member type declaration. Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public. Use of the access modifiers, private or protected, is contradictory and results in a compile-time error. In contrast, the modifiers, private and protected, are applicable to a member type declaration appearing within the body of a directly enclosing class. The modifier, final, is never applicable to an interface. The keyword, implements, is not a modifier.

    Rate this question:

  • 4. 

    interface A { int a = 1; // 1 public int b = 2; // 2 public static int c = 3; // 3 public static final int d = 4; // 4 } Which field declaration results in a compile-time error?

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      3,4

    • E.

      4

    • F.

      None of the above

    Correct Answer
    F. None of the above
    Explanation
    All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface.

    Rate this question:

  • 5. 

    Which of the following are not methods of the java.lang.String class?

    • A.

      Append

    • B.

      Concat

    • C.

      Delete

    • D.

      Insert

    • E.

      Substring

    Correct Answer(s)
    A. Append
    C. Delete
    D. Insert
    Explanation
    The StringBuffer class has methods named append, delete and insert, but the String class does not. A typical trick question will attempt to invoke StringBuffer methods on a String instance

    Rate this question:

  • 6. 

    class MWC106 { static void m1(String s) { s = s.trim(); s = s.concat("D"); } public static void main(String[] s) { String s1 = "A", s2 = " B ", s3 = "C"; m1(s2); System.out.print(s1 + s2 + s3); }} What is the result of attempting to compile and run the program?

    • A.

      Prints: A B C

    • B.

      Prints: ABC

    • C.

      Prints: A B CD

    • D.

      Compile-time error

    • E.

      None of the above

    Correct Answer
    A. Prints: A B C
    Explanation
    The String instance referenced by s2 is passed to the m1 method by passing the value of the reference. The reference value used in method m1 is a local copy of the reference. If the local copy used in method m1 is changed, then the original reference variable in the main method remains unchanged.

    Rate this question:

  • 7. 

    What is the result of compiling and running this program? class Mammal{ void eat(Mammal m){ System.out.println("Mammal eats food"); } } class Cattle extends Mammal{ void eat(Cattle c){ System.out.println("Cattle eats hay"); } } class Horse extends Cattle{ void eat(Horse h){ System.out.println("Horse eats hay"); } } public class Test{ public static void main(String[] args){ Mammal h = new Horse(); Cattle c = new Horse(); c.eat(h); } }

    • A.

      Prints "Mammal eats food"

    • B.

      Prints "Cattle eats hay"

    • C.

      Prints "Horse eats hay"

    • D.

      Class cast Exception at runtime

    Correct Answer
    A. Prints "Mammal eats food"
    Explanation
    The method that will be called is the one
    from class Mammal. The reasons are quite obvious.

    Rate this question:

  • 8. 

    class MWC117 { public static void main (String[] args) { System.out.print(String.valueOf(1) + String.valueOf(2)); String s1 = "S1"; String s2 = s1.toString(); System.out.print("," + (s1==s2)); }} What is the result of attempting to compile and run the program?

    • A.

      Prints: 3,false

    • B.

      Prints: 3,true

    • C.

      Prints: 12,false

    • D.

      Prints: 12,true

    • E.

      None of the above

    Correct Answer
    D. Prints: 12,true
    Explanation
    The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object, char[], or any primitive type. The toString method returns a reference to the existing String instance. It does not create a new instance of the String.

    Rate this question:

  • 9. 

    class MWC200 { public static void main (String[] args) { String s1 = "ABC"; StringBuffer s2 = new StringBuffer(s1); System.out.print(s2.equals(s1) + "," + s1.equals(s2)); }} What is the result of attempting to compile and run the program?

    • A.

      Prints: false,false

    • B.

      Prints: true,false

    • C.

      Run-time error

    • D.

      Prints: true,true

    Correct Answer
    A. Prints: false,false
    Explanation
    StringBuffer.equals does not override the Object.equals method; so StringBuffer.equals just compares reference values. Since the reference variables s1 and s2 refer to different objects, the equals method of the StringBuffer instance s2 returns the value false. The String.equals method does override the Object.equals, method. The String.equals method returns false anytime the argument is not an instance of type String. The method invocation expression s1.equals(s2) produces the value false, because the argument is an instance of type StringBuffer

    Rate this question:

  • 10. 

    import java.util.*; class GFC112 { public static void main (String[] args) { Object m = new LinkedHashSet(); System.out.print((m instanceof Collection)+","); System.out.print((m instanceof Set)+","); System.out.print(m instanceof List); }} What is the result of attempting to compile and run the program?

    • A.

      Prints: false,false,false

    • B.

      Prints: false,false,true

    • C.

      Prints: false,true,false

    • D.

      Prints: true,true,false

    • E.

      Prints: true,true,true

    Correct Answer
    D. Prints: true,true,false
    Explanation
    LinkedHashSet does not implement the List interface. LinkedHashSet extends HashSet and implements Collection, Set, Cloneable and Serializable

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 01, 2012
    Quiz Created by
    Venkatasanthosh
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.