Ocjp Mock Test - I

46 Questions | Total Attempts: 69

SettingsSettingsSettings
Mock Test Quizzes & Trivia

INTRODUCTION TO OOPS INTRODUCTION TO JAVA AND SDE LANGUAGE FUNDAMENTALS AND OPERATORS


Questions and Answers
  • 1. 
    Consider the following code: 1. class Test { 2. public static void main(String args[]) { 3. double d = 12.3; 4. Dec dec = new Dec(); 5. dec.dec(d); 6. System.out.println(d); 7. } 8. } 9. class Dec{ 10. public void dec(double d) { d = d - 2.0d; } 11. } Which of the following gives the correct value printed at line 6?
    • A. 

      Prints: 12.3

    • B. 

      Prints: -2.0

    • C. 

      Prints: 10.3

    • D. 

      Prints: 0.0

  • 2. 
    Consider the following code: public class LabeledBreak2 { public static void main(String args[]) { loop: for(int j=0; j<2; j++) { for(int i=0; i<10; i++) { if(i == 5) break loop; System.out.print(i + " "); } } } } Which of the following will be the output for the above code?
    • A. 

      0 1 2 3 4 5

    • B. 

      Indefinite Loop

    • C. 

      1 2 3 4 5

    • D. 

      0 1 2 3 4

    • E. 

      0 1 2 3 4 0 1 2 3 4

  • 3. 
    Consider the following scenario: Real Chocos Private Limited deals in manufacturing variety of chocolates. This organization manufactures three varieties of chocolates. 1. Fruit Chocolates 2. Rum Chocolates 3. Milk Chocolates A software system needs to be built. Which of the following options identifies the Classes and Objects?
    • A. 

      Class: Real Chocos Private Limited Objects: Chocolate

    • B. 

      Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates

    • C. 

      Class: Chocolate Objects: Milk Chocolates

    • D. 

      Class: Fruit Chocolates Objects: Rum Chocolates

  • 4. 
    Consider the following code: class A { } class B extends A { } public class Code2 { public void method(A a) { System.out.println("A"); } public void method(B b) { System.out.println("B"); } public static void main(String args[]) { new Code2().method(new Object()); } } Which of the following will be the output for the above code?
    • A. 

      Throws ClassCastException at runtime

    • B. 

      Prints: B

    • C. 

      Compilation Error 'Cannot find the symbol'

    • D. 

      Prints: A

    • E. 

      Return this.name.hashCode() == a.name.hashCode();

  • 5. 
    Consider the following code: class Planet { } class Earth extends Planet { } public class WelcomePlanet { public static void welcomePlanet(Planet planet)  { if (planet instanceof Earth) { System.out.println("Welcome!"); }  else if (planet instanceof Planet)  { System.out.println("Planet!"); } else { System.exit(0); } } public static void main(String args[])  { WelcomePlanet wp = new WelcomePlanet(); Planet planet = new Earth(); welcomePlanet(planet); } } Which of the following will be the output of the above program?
    • A. 

      An exception is thrown at runtime

    • B. 

      Planet!

    • C. 

      The code runs with no output

    • D. 

      Welcome!

    • E. 

      Compilation fails

  • 6. 
    Consider the following code:  public class Code13 { public static void main(String... args)  { for(String s:args) System.out.print(s + ", "); System.out.println(args.length); } }  Which of the following will be the output if the above code is attempted to compile and execute?
    • A. 

      Program compiles successfully and prints the passed arguments as comma separated values and finally prints the length of the arguments-list

    • B. 

      Runtime Error: NoSuchMethodError

    • C. 

      Variable arguments cannot be used with enhanced for-loop

    • D. 

      Compilation Error: var-args cannot be used as arguments for main() method

  • 7. 
    Consider the following code: class UT1 { static byte m1() { final char c = 'u0001'; return c; } static byte m3(final char c) { return c; } public static void main(String[] args) { char c = 'u0003'; System.out.print(""+m1()+m3(c)); } } Which of the following gives the valid output of the above code?
    • A. 

      Compile-time error

    • B. 

      Prints: 13

    • C. 

      Run-time error

    • D. 

      Prints: 4

    • E. 

      None of the listed options

  • 8. 
    Which are all platform independent among the following? (Choose 3)
    • A. 

      JAR Files

    • B. 

      Java Virtual Machine (JVM)

    • C. 

      Java Development Kit (JDK)

    • D. 

      Java Class Files

    • E. 

      Java Source Files

  • 9. 
    Consider the following partial code: public class CreditCard { private String cardID; private Integer limit; public String ownerName; public void setCardInformation(String cardID, String ownerName, Integer limit) { this.cardID = cardID; this.ownerName = ownerName; this.limit = limit; } } Which of the following statement is True regarding the above given code?
    • A. 

      The class is fully encapsulated

    • B. 

      The setCardInformation method breaks encapsulation

    • C. 

      The code demonstrates polymorphism

    • D. 

      The cardID and limit variables break polymorphism

    • E. 

      The ownerName variable breaks encapsulation

  • 10. 
    Consider the following code: 1. public class DagRag { 2. public static void main(String [] args) { 3. 4. int [][] x = new int[2][4]; 5. 6. for(int y = 0; y < 2; y++) { 7. for(int z = 0; z < 4; z++) { 8. x[y][z] = z; 9. } 10. } 11. 12. dg: for(int g = 0; g < 2; g++) { 13. rg: for(int h = 0; h < 4; h++) { 14. System.out.println(x[g][h]); 15. 16. } 17. System.out.println("The end."); 18. 19. } 20. 21. } 22. } Which of the following code snippet when inserted at lines 15 and 18 respectively, will make the above program to generate the below output? 0 1 2 3 The end.
    • A. 

      If(g==3) break rg; if(h==0) break dg;

    • B. 

      If(h > 3) break dg; if(g > 0) break rg;

    • C. 

      If(h==3) break rg; if(g==0) break dg;

    • D. 

      If(h > 3) break dg; if(g > 0) break dg;

  • 11. 
    Consider the following code snippet: class Animal { String name; public boolean equals(Object o) { Animal a = (Animal) o; // Code Here } } class TestAnimal { public static void main(String args[]) { Animal a = new Animal(); a.name = "Dog"; Animal b = new Animal(); b.name = "dog"; System.out.println(a.equals(b)); } } Which of the following code snippets should be replaced for the comment line (//Code Here) in the above given code, to get the output as true?
    • A. 

      Return this.name.equalsIgnoreCase(a.name);

    • B. 

      Return this.name.equals(a.name);

    • C. 

      Return super.equals(a);

    • D. 

      Return this.name == a.name;

  • 12. 
    Consider the following code snippet: public class TestString9 { public static void main(String st[]){ String s1 = "java"; String s2 = "java"; String s3 = "JAVA"; s2.toUpperCase(); s3.toUpperCase(); boolean b1 = s1==s2; boolean b2 = s1==s3; System.out.print(b1); System.out.print(" "+b2); } } What will be the output of the above code snippet?
    • A. 

      False true

    • B. 

      True true

    • C. 

      True false

    • D. 

      Runtime error

    • E. 

      false false

  • 13. 
    Consider the following code snippet: public class Welcome { String title; int value; public Welcome() { title += " Planet"; } public void Welcome() { System.out.println(title + " " + value); } public Welcome(int value) { this.value = value; title = "Welcome"; Welcome(); } public static void main(String args[]) { Welcome t = new Welcome(5); } } Which of the following option will be the output for the above code snippet?
    • A. 

      Compilation fails

    • B. 

      Welcome Planet 5

    • C. 

      Welcome 5

    • D. 

      Welcome Planet

    • E. 

      The code runs with no output

  • 14. 
    Consider the following code: class ExampleFive { public static void main(String[] args) { final int i = 22; byte b = i; System.out.println(i + ", " + b); } } Which of the following gives the valid output for the above code?
    • A. 

      Prints: 22, 20

    • B. 

      Runtime Error: Cannot type cast int to byte

    • C. 

      Prints: 22, 22

    • D. 

      Compile Time Error: Loss of precision

  • 15. 
    Consider the following code snippet: class TestString2 { public static void main(String args[]) { String s1 = "Test1"; String s2 = "Test2"; s1.concat(s2); System.out.println(""+s1.charAt(s1.length() - 3) + s1.indexOf(s2)); } } What will be the output of the above code snippet?
    • A. 

      S5

    • B. 

      S-1

    • C. 

      T4

    • D. 

      15

    • E. 

      T4

  • 16. 
    Consider the following code: 1. public class SwitchIt { 2. public static void main(String[] args) 3. int w1 = 1; 4. int w2 = 2; 5. System.out.println(getW1W2(w1, w2)); 6. } 7. 8. public static int getW1W2(int x, int y) { 9. switch (x) { 10. case 1: x = x + y; 11. case 2: x = x + y; 12. } 13. return x; 14. } 15. } Which of the following gives the valid output of above code?
    • A. 

      Compilation succeeds and the program prints "5"

    • B. 

      Compilation fails because of an error on line 9

    • C. 

      Compilation succeeds and the program prints "3"

    • D. 

      Compilation fails because of errors on lines 10 and 11

  • 17. 
    Consider the following code snippet: class Train { String name = "Shatapdhi"; } class TestTrain { public static void main(String a[]) { Train t = new Train(); System.out.println(t); // Line a System.out.println(t.toString()); // Line b } } Which of the following statements are true?(Choose 3)
    • A. 

      Line a prints the corresponding classname with Object's hashcode in Hexadecimal

    • B. 

      Both Line a and Line b prints "Shatapdhi"

    • C. 

      Both Line a and Line b will print the corresponding classname with Object's hashcode in Hexa Decimal

    • D. 

      Output of Line a and Line b will be different

    • E. 

      Output of Line a and Line b will be same

  • 18. 
    Which of the following are true about inheritance?(Choose 3)
    • A. 

      In an inheritance hierarchy, a subclass can also act as a super class

    • B. 

      Inheritance enables adding new features and functionality to an existing class without modifying the existing class

    • C. 

      Inheritance enables adding new features and functionality to an existing class without modifying the existing class

    • D. 

      Inheritance is a kind of Encapsulation

    • E. 

      Inheritance does not allow sharing data and methods among multiple classes

  • 19. 
    Consider the following code: 1 public class A { 2 public void m1() {System.out.print("A.m1, ");} 3 protected void m2() {System.out.print("A.m2, ");} 4 private void m3() {System.out.print("A.m3, ");} 5 void m4() {System.out.print("A.m4, ");} 6 7 public static void main(String[] args) { 8 A a = new A(); 9 a.m1(); 10 a.m2(); 11 a.m3(); 12 a.m4(); 13 } 14 } Which of the following gives the lines that need to be removed in order to compile and run the above code correctly?
    • A. 

      Lines 10, 11

    • B. 

      No need to comment any line. Will compile and run

    • C. 

      Lines 10, 11 and 12

    • D. 

      Line 11

  • 20. 
    Which of the following options gives the difference between == operator and equals() method?
    • A. 

      Equals compares hash value and == compares character sequence

    • B. 

      No difference;they are essentially the same

    • C. 

      If equals() is true then == is also true

    • D. 

      ==compares object's memory address but equals character sequence

    • E. 

      == works on numbers equals() works on characters

  • 21. 
    Which of the following classes is new to JDK 1.6?
    • A. 

      Java.io.File

    • B. 

      Java.io.Serializable

    • C. 

      Java.io.FileFilter

    • D. 

      Java.io.Console

    • E. 

      Java.io.Externalizable

  • 22. 
    Which of the following statements are correct regarding Static Blocks?(Choose 3)
    • A. 

      A class that have static block, should have the main() method defined in it

    • B. 

      A class can have more than one static block

    • C. 

      Static blocks are executed only once

    • D. 

      Static blocks are executed before main() method

    • E. 

      A static block can call other methods in a class

  • 23. 
    Which of the following flow control features does Java support? (Choose 2)
    • A. 

      Labeled continue

    • B. 

      Labeled goto

    • C. 

      Labeled throw

    • D. 

      Labeled catch

    • E. 

      Labeled break

  • 24. 
    Which of the following options gives the relationship between a Spreadsheet Object and Cell Objects?
    • A. 

      Polymorphism

    • B. 

      Association

    • C. 

      Aggregation

    • D. 

      Inheritance

    • E. 

      Persistence

  • 25. 
    Consider the following program: public class D extends Thread { public void run() { System.out.println("Before start method"); this.stop(); System.out.println("After stop method"); } public static void main(String[] args) { D a = new D(); a.start(); } } What will be the output of the above program?
    • A. 

      'Before start method' and 'After stop method'

    • B. 

      Compilation error

    • C. 

      'Before start method' only

    • D. 

      Runtime exception