Attempting To Compile And Run The Java Program

10 Questions | Total Attempts: 58

SettingsSettingsSettings
Please wait...
Attempting To Compile And Run The Java Program

.


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

  • 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

  • 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

  • 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

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

      Append

    • B. 

      Concat

    • C. 

      Delete

    • D. 

      Insert

    • E. 

      Substring

  • 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

  • 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

  • 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

  • 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

  • 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