Ocjp Mock Test - I

46 Questions

Settings
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

  • 26. 
    Consider the following code: public class Choco { Choco() { System.out.print("Choco"); } class Bar { Bar() { System.out.print("bar"); } public void go() { System.out.print("sweet"); } } public static void main(String[] args) { Choco c = new Choco(); c.makeBar(); } void makeBar(){ // Insert code here } } Which of the following code snippet when substituted individually to the above commented line (// Insert code here) will give the following output?
    • A. 

      New Choco().go();

    • B. 

      New Bar().go();

    • C. 

      New Choco(). new Bar().go();

    • D. 

      (new Bar() {}).go();

    • E. 

      Go();

  • 27. 
    Consider the following code: public class SwitchIt { public static void main(String args[]) { int x = 10; switch(x) { case 10: for(int i=0; i break; case 20: System.out.println(x); break; case 30: System.out.println(x*2); break; default: System.out.println(x*3); } } } Which of the following will be the output for the above program?
    • A. 

      10

    • B. 

      11

    • C. 

      No output

    • D. 

      30

  • 28. 
    Consider the following code: class One { public One() { System.out.print(1); } } class Two extends One { public Two() { System.out.print(2); } } class Three extends Two { public Three() { System.out.print(3); } } public class Numbers { public static void main(String[] argv) { new Three(); } } Which of the following will be the output for the above program?
    • A. 

      321

    • B. 

      No output

    • C. 

      123

    • D. 

      32

    • E. 

      3

  • 29. 
    Consider the following code: public class TestOne { public static void main(String args[]) { byte x = 3; byte y = 5; System.out.print((y%x) + ", "); System.out.println(y == ((y/x) *x +(y%x))); } } Which of the following gives the valid output for above?
    • A. 

      Prints: 1, true

    • B. 

      Prints 2, false

    • C. 

      Prints: 1, false

    • D. 

      Prints: 2, true

  • 30. 
    Consider the following code: 1. public class Garment { 2. public enum Color { 3. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 4. private final int rgb; 5. Color( int rgb) { this.rgb = rgb; } 6. public int getRGB() { return rgb; } 7. }; 8. public static void main( String[] argv) { 9. // insert code here 10. } 11.} Which of the following code snippets, when inserted independently at line 9, allow the Garment class to compile? (Choose 2)
    • A. 

      Color treeColor = Color.GREEN;

    • B. 

      Color purple = new Color( 0xff00ff);

    • C. 

      Color skyColor = BLUE;

    • D. 

      If( RED.getRGB() < BLUE.getRGB() ) {}

    • E. 

      If( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}

  • 31. 
    Consider the following code: class Alpha { protected Beta b; } class Gamma extends Alpha { } class Beta { } Which of the following statement is True?
    • A. 

      Beta has-a Gamma and Gamma is-a Alpha.

    • B. 

      Gamma has-a Beta and Gamma is-a Alpha

    • C. 

      Alpha is-a Gamma and has-a Beta.

    • D. 

      Gamma is-a Beta and has-a Alpha.

    • E. 

      Alpha has-a Beta and Alpha is-a Gamma

  • 32. 
    Consider the following code snippet:   String deepak = "Did Deepak see bees? Deepak did.";   Which of the following method calls would refer to the letter b in the string referred by the variable deepak?
    • A. 

      CharAt(13)

    • B. 

      CharAt(15)

    • C. 

      CharAt(12)

    • D. 

      CharAt(16)

    • E. 

      CharAt(14)

  • 33. 
    Which of the following codes will compile and run properly?
    • A. 

      Public class Test2 { static public void main(String[] in) { System.out.println("Test2"); } }

    • B. 

      Public class Test4 { static int main(String args[]) { System.out.println("Test4"); } }

    • C. 

      public class Test1 { public static void main() { System.out.println("Test1"); } }

    • D. 

      Public class Test3 { public static void main(String args) { System.out.println("Test3"); } }

    • E. 

      Public class Test5 { static void main(String[] data) { System.out.println("Test5"); } }

  • 34. 
    Consider the following code: 1. public class Circle1 { 2. private String string = "String1"; 3. void work() { 4. String x = "String2"; 5. class Circle2 { 6. public void peepOut() { 7. System.out.println(string); 8. System.out.println(x); 9. } 10. } 11. new Circle2().peepOut(); 12. } 13. 14. public static void main(String args[]) { 15. Circle1 c1 = new Circle1(); 16. c1.work(); 17. } 18. } Which of the following changes made to the above code will make the code to compile and execute properly and gives the following output? String1 String2
    • A. 

      The variable at line 4 should be declared as final

    • B. 

      The variable at line 2 should be declared as final

    • C. 

      The method at line 6 should be defined as final method

    • D. 

      The inner class Circle 2 should be an abstract class

    • E. 

      The object for the inner class Circle2 should be created in main() method

  • 35. 
    Which of the following options gives the relationship between a Pilot class and Plane class?
    • A. 

      Inheritance

    • B. 

      Polymorphism

    • C. 

      Persistence

    • D. 

      Aggregation

    • E. 

      Association

  • 36. 
    Consider the following code: public class Key1 { public boolean testAns( String ans, int n ) { boolean rslt; if (ans.equalsIgnoreCase("YES") & n > 5) rslt = true; return rslt; } public static void main(String args[]) { System.out.println(new Key1().testAns("no", 5)); } } Which of the following will be the output of the above program?
    • A. 

      Compile-time error

    • B. 

      Runtime Error

    • C. 

      NO

    • D. 

      False

    • E. 

      True

  • 37. 
    Which of the following methods are defined in Object class? (Choose 3)
    • A. 

      ToString()

    • B. 

      CompareTo(Object)

    • C. 

      HashCode()

    • D. 

      Equals(Object)

    • E. 

      Run()

  • 38. 
    Consider the following code: public class Code4 { private int second = first; private int first = 1000; public static void main(String args[]) { System.out.println(new Code4().second); } } Which of the following will be the output for the above code?
    • A. 

      Throws a Runtime error 'Illegal forward reference'

    • B. 

      1000

    • C. 

      Compiler complains about forward referencing of member variables first and second

    • D. 

      Compiler complains about private memebers is not accessible from main() method

  • 39. 
    Consider the following code: public class ObParam{ public int b = 20; public static void main(String argv[]){ ObParam o = new ObParam(); methodA(o); } public static void methodA(ObParam a) { a.b++; System.out.println(a.b); methodB(a); System.out.println(a.b); } public void methodB(ObParam b) { b.b--; } } Which of the following gives the correct output for the above code?
    • A. 

      Prints: 21 21

    • B. 

      Prints: 20 21

    • C. 

      Compilation Error: Non-static method methodB() cannot be referenced from static context methodA()

    • D. 

      Prints: 20 20

    • E. 

      Prints: 21 20

  • 40. 
    Which of the following statements are true? (Choose 2)
    • A. 

      All exceptions are thrown programmatically from the code or API

    • B. 

      All exceptions are thrown by JVM

    • C. 

      JVM cannot throw user-defined exceptions

    • D. 

      JVM thrown exceptions can be thrown programmatically

    • E. 

      All RuntimeException are thrown by JVM

  • 41. 
    Delimiters themselves be considered as tokens. State True or False.
    • A. 

      True

    • B. 

      False

  • 42. 
    Consider the following code: public class ExampleSeven { public static void main(String [] args) { String[] y = new String[1]; String x = "hello"; y[0] = x; // Code here System.out.println("match"); } else { System.out.println("no match"); } } } Which of the following code snippet when substituted at the commented line (// Code here) in the above code will make the program to print "no match"?
    • A. 

      If (x != y[0].toString()) {

    • B. 

      If (x & y[0]) {

    • C. 

      If (x.equals(y[0])) {

    • D. 

      If (!x.equals(y[0])) {

  • 43. 
    Consider the following code: class Animal { public String noise() { return "noise"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return "meow"; } } class MakeNoise { public static void main(String args[]) { Animal animal = new Dog(); Cat cat = (Cat)animal; System.out.println(cat.noise()); } } Which of the following option will be the output of the above code snippet?
    • A. 

      Noise

    • B. 

      Bark

    • C. 

      Meow

    • D. 

      Compilation fails

    • E. 

      An exception is thrown at runtime

  • 44. 
    Consider the following code: public class GetArray { public static void main(String args[]) { float invt[][]; float[] prct, grts[]; float[][] sms, hms[]; (// Insert statement1 here) (// Insert statement2 here) (// Insert statement3 here) } } Which of the following listed statements can be inserted at the above commented lines (// Insert statement1 here, // Insert statement2 here, // Insert statement3 here) to make the program to compile without errors? (Choose 3)
    • A. 

      Grts = new float[1][4];

    • B. 

      Invt = grts;

    • C. 

      Hms = new float[2][5];

    • D. 

      Invt = new float[4][2];

    • E. 

      Grts = new float[1];

  • 45. 
    Which of the following statements are correct regarding Instance Block?(Choose 3)
    • A. 

      A class can have more than one instance block

    • B. 

      An instance block cannot initialise the class members

    • C. 

      Instance blocks are executed only when the instances are created from main() method of that class

    • D. 

      Instance blocks are executed before constructors

    • E. 

      Instance blocks are executed for every created instance

  • 46. 
    Which one do you like?
    • A. 

      Option 1

    • B. 

      Option 2

    • C. 

      Option 3

    • D. 

      Option 4