OCPJP 6 Mock Exam – 7

SHARE & COMMENT :

1 . Consider the following line of code:

public class Test{
   public void main(){
   System.out.println("Hi");
}
public static void main (String [] args)
{
Test t=new Test();
t.main();
}
}

What will be happen if you compile and run above code?

  1. It will not compile
  2. It will not run
  3. It will compile but will not run
  4. It will output “Hi”

2 . After execution of the code fragment below, what are the value of the variables x1, y1, and z1?

int x=10; int y =10; int z=10; int x1, y1, z1;
x1=++y;
y1=z++;
z1=z;

Choose the one below:

  1. x1 = 10 , y1 = 10 and z1=10
  2. x1 = 10, y1 = 11, and z1=10
  3. x1=10, y1 = 10, and z1=11
  4. x1=11, y1=10, and z1=11

3 . What will be the output?

Class Test{
public int addTest(int x, int y)
{
      x=x+1; y=y+1;
      int z=(x+y);
      return z;
}
public static void main(String [] args)
{
      int x=10; int y=10; int z=0;
      Test t=new Test();
      z= t.addTest(x,y);
      System.out.println("x="+x+", y="+y+", z="+z);
}
}

Choose the one below:

  1. x=10, y=10, z=22
  2. x=11, y=11, z=22
  3. x=10, y=10, z=20
  4. x=11, y=11, z=20

4 . What will be the output of the program.?
Assume that MyList class is declared in MyList.java
and ListManager class is declared in ListManager.java file.

public class MyList{
int size=1;
public static void main(String [] args)
{
      MyList list=new MyList();
     list.size=10;
     ListManager lm=new ListManager();
     lm.expandList(list);
    System.out.println("list.size="+list.size);
}
}  //end of MyList
public class ListManager{
public void expandList(MyList l)
{
     l.size=l.size+10;
}
}//end of ListManager

Choose the one below:

  1. list.size=0
  2. list.size=10
  3. list.size=11
  4. list.size=20

5 . If int x = -1 then which of the following expression results in a positive value in x?

  1. x=x>>>32
  2. x=x>>>5
  3. x=x>>5
  4. x=~x

6 . Which of the following lines of code would print “Equal” when you run it?

  1. int x=1; float y=1.0F; if(x==y){ System.out.println(“Equal”);}
  2. int x=1; Integer y= new Integer(1); if(x==y) { System.out.println(“Equal”);}
  3. Integer x=new Integer(1); Integer y=new Intger(1); if(x==y){ System.out.println(“Equal”);}
  4. String x=”1″; String y=”1″; if (x==y) { System.out.println(“Equal”);}

7 . Which of the following declarations are correct for the top level class?

  1. public synchronized class MyTest extends Thread
  2. private class MyTest extends Thread
  3. public abstract class MyTest extends Thread
  4. class MyTest extends Thread

8 . Consider the following lines of code:

class Test{
String x;
public void testDemo(int n)
{
       String y;
       if ( n>0) {
              y="Hello";
       }
       System.out.println(x+y);
}
public static void main(String [] args)
{
          Test test=new Test();
          test.testDemo(2);
}
}

What will happen if you try to compile and run above code?

  1. It will produce compiler warning that variable y may not have been initialized
  2. It will produce compiler warning that variable x may not have been initialized
  3. It will output “Hello”
  4. It will output “nullHello”

9 . Consider that Parent and Child classes are defined in two different files as below:

class Parent{
public Parent(){
System.out.println("I am Parent");
}
}
class Child extends Parent{
public Child(int x){
System.out.println("I am Child");
}
public static void main(String [] args){
Child c=new Child(10);
}
}

What will be output if you try to compile and run above code?

  1. It will not compile.
  2. It will compile successfully. It will output “I am Parent” and then “I am Child.”
  3. It will compile successfully. It will output “I am Child” and then “I am Parent.”
  4. It will compile successfully, but will not run.

10 . Consider following code:

public class MyList{
static int size;
public expandList(int newSize){
ListExpander lexp=new ListExpander();
Vector expandedList=lexp.expand();
     class ListExpander{
     public Vector expand(){
        Vector v=new Vector(this.size+newSize);
        return v;
     }
     }
}
}

What will happen if you attempt to compile above code?

  1. compiler error, “cannot refer inside an inner class to a static variable.”
  2. compiler error, “cannot refer inside an inner class to to a non-final variable newSize defined in a different method.”
  3. Both of the above
  4. None of the above

11 . Consider following code:

public class Parent{
public  int size =0;
static class InnerClass{
public void incrementParentSize(){
    XXX=XXX+10;
}
}
}

In above code, how can you access ‘size’ variable (of outer class Parent) inside innerclass at the place of ‘XXX’ ?

  1. super.size
  2. this.size
  3. Parent.size
  4. Can not access it

12 . Assume that Parent and Child classes are in different files:

public class Parent{
public Parent(int x, int y)
{
       System.out.println("Created Parent");
}
}//end of Parent class
public class Child extends Parent{
public Child(int x, int y){
//
}
public Child(int x, int y, int z){
System.out.println("Creating child");
this(x,y);
}
public static void main(String [] args){
Child c=new Child(1,2,3);
}
}

What will happen if you try to compile and run above program?

  1. It will compile successfully. It will output “Created Parent” and then “Creating child”
  2. It will compile successfully. It will output “Creating child” and then “Created Parent”
  3. It will not compile giving warning, “Explicit constructor invocation must be first statement in constructor.”
  4. It will not compile giving warning, “Expression is not a valid block statement.”

13 . Consider following code:

public class OuterClass{
class InnerClass{
}
public void innerClassDemo(){
//Explicit instance of InnerClass
}
}

In above code, how can you explicitly create an instance of InnerClass?

  1. InnerClass i=InnerClass();
  2. InnerClass i=OuterClass.InnerClass();
  3. InnerClass i=new OuterClass ().new InnerClass();
  4. OuterClass.InnerClass i=new OuterClass.InnerClass();

14 . Please select valid array declaration(s):

  1. int x[20];
  2. int []x=new int[20];
  3. int [][] x=new int [20][];
  4. int [][][] x=new int[20][20][20];
  5. int [] x={1,2,3};

15 . Consider following code:

public class Test{
protected void demo() throws NumberFormatException, ArrayIndexOutOfBoundsException {
//something here
}
public void demo(String s){
//something here
}
}//end of Test class

Please select true statement(s) for demo code?

  1. It is an example of overloading method
  2. It is an example of overriding method
  3. Both of the above
  4. None of the above

16 . For the following code, please consider that super class is defined in question #15:

public class MyTest extends Test{
private void demo() throws IndexOutOfBoundsException, ClassNotFoundException
{
//something here
}
}//end of MyTest class

What will happen if you try to compile above code ?

  1. It will compile successfully.
  2. Compiler error: Exception java.lang.ClassNotFoundException in throws clause of void MyTest.demo() is not compatible with void Test.demo().
  3. Compiler error: Cannot reduce visibility of the inherited method from Test.
  4. Both B and C

17 . Consider the following code:

public class Test{
public void demo(String [] list){
try{
         String s=list[list.length+1];
        System.out.println(s);
    }catch(ArrayIndexOutOfBoundException e){
   return;
  }finally{
      System.out.println("Finally here.");
}
public static void main(String [] args){
Test t=new Test();
String [] list={"one","two"};
t.demo(list);
System.out.println("Done!");
}
}//end of Test class

What happen if you try compile and run above code ?

  1. It will not compile.
  2. It will output “null” and then “Finally here.”
  3. It will output “Done!”
  4. It will output “Finally here” and then “Done!”

18 . Please consider following code:

public class Test{
public static void demo(String s)
{
      debug("In demo:"+s);
}
private void debug(String s){
System.out.println(s);
}
public static void main(String [] args){
Test.demo("Hello");
}
}

What will happen if you try to compile and run above code ?

  1. It will compile successfully, but will not run.
  2. It will compile successfully, and outputs “In demo:Hello.”
  3. It will not compile with error message “Can not make a static reference to the instance method named.”
  4. None of the above

19 . Consider the following code:

/** File Drawable.java */
public interface Drawable{
public void draw();
public void  fill();
} /** End of file Drawable.java*/

/** File Circle.java */
public class Circle implements Drawable{
int center=0;
public void draw(){
System.out.println("Drawing circle");
}
public static void main(String [] args){
Circle c=new Circle();
c.draw();
}
} /** End of file Circle.java */

If you attempt to compile and run Circle class what will be output?

  1. It will compile successfully, and outputs “Drawing circle.”
  2. It will not compile, and reports error: “class Circle must implement inherited abstract method void Drawable.fill.”
  3. It will not compile, and reports error: “Method Drawable.fill requires a body instead of a semicolon.”
  4. None of the above

20 . What will be the output?

int x=2; int y=3; int z=4;
if(x>2){
    System.out.println("Tested x");
}if(y<3){
   System.out.println("Tested y");
}if (z<=3){
   System.out.println("Tested z");
}

Choose the one below:

  1. Tested x.
  2. Tested y.
  3. Tested z.
  4. None of the above.

21 . Consider the following code:

for( int i=0;i<2;i++)
{
      for(int j=i;j<3; j++)
     {
             if (i==j)
             {
                   continue;
             }
             System.out.println("i="+i+" j="+j);
     }
}

Which lines would be part of the output?

  1. i = 0 j = 1
  2. i = 0 j = 2
  3. i = 1 j = 2
  4. None of the above

22 . Consider the following code:

int j=0;
for( int i=0;i<2;i++)
{
      for (j=i; j<3; j++)
     {
              continue;
     }
 System.out.println("i = "+i+" j = "+j);
}

Which lines would be part of the output?

  1. i = 0 j = 0
  2. i = 1 j = 1
  3. i = 0 j = 3
  4. i = 1 j =3

23 . Consider the following code:

int i=0; int j=0;
for( i=0;i<2;i++)
{
      for (j=i; j<3; j++)
     {
              break;
     }
  System.out.println("i = "+i+" j = "+j);
}

Which lines would be part of the output?

  1. i = 0 j = 0
  2. i = 1 j = 1
  3. i = 0 j = 3
  4. i = 1 j = 3

24 . Consider the following code:

int i, j=0;
outer:
for( i=0;i<2;i++)
{
      for (j=i; j<3; j++)
     {
              continue outer;
     }
  System.out.println("i = "+i+" j = "+j);
}

Which lines would be part of the output?

  1. i = 0 j = 0
  2. i = 1 j = 1
  3. i = 0 j = 3
  4. None of the above

25 . Consider the following code:

int i, j=0;

for( i=0;i<2;i++)
{
      inner:
      for ( j=i; j<3; j++)
     {
              break inner;
     }
  System.out.println("i = "+i+" j = "+j);
}

Which lines would be part of the output?

  1. i = 0 j = 0
  2. i = 1 j = 1
  3. i = 0 j = 3
  4. None of the above

26 . What will be the output?

Thread currentThread=Thread.currentThread();
int priority = currentThread.getPriority();
Thread t1=new Thread();
t1.setPriority(9);
ThreadGroup tgrp=new ThreadGroup();
tgrp.setMaxPriority(10);
Thread t2=new Thread(tgrp,"t2");
System.out.println("Priority of t1="+t1.getPriority());
System.out.println("Priority of t2="+t2.getPriority());

Choose the one below:

  1. Priority of t1=5 and Priority of t2=10
  2. Priority of t1=9 and Priority of t2=10
  3. Priority of t1=9 and Priority of t2=5
  4. Neither of above

27 . Consider the following code:

/** File Thread1.java */
class Thread1 implements Runnable{
public void run(){
System.out.println("Running Thread1");
}}  /** End of file Thread1.java */

/** Thread2.java */
class Thread2 extends Thread{
public void run(){
System.out.println("Running Thread2");
}
public static void main(String [] args){
Thread1 t1= new Thread1();
Thread t2=new Thread2(t1);
t1.start();
t2.start();
}
}
/** End of Thread2.java*/

If you try to compile and run above code what will be result?

  1. “Running thread1″ following “Running thread2″
  2. “Running thread2″ following “Running thread1″
  3. It will not compile because in Thread1 and Thread2 start() is not defined .
  4. It will not compile because constructor invoked to create Thread2 with arguments (Thread1) is not defined

28 . Consider the following code:

class MyThread extends Thread{
public void run(){
System.out.println("Done");
}
public void demo(){
System.out.println("Demo");
}
public static void main(String args[]){
MyThread th=new MyThread();
th.run();
th.stop();
th.demo();
}
}

What will happen if you try to compile and run above code?

  1. It will throw an exception at th.run() because run() was called before calling start().
  2. It will throw an exception at th.demo() because Thread variable th was already stopped calling stop().
  3. It will output “Done” following “Demo”
  4. Neither of the above.

29 . Please consider following code:

String s1=" 5 + 5 = 10 ";
s1.trim();
s1.replace('+', '-');

How many String objects will be created after executing above code?

  1. 1
  2. 2
  3. 3
  4. 4

30 . What will be the output?

String s="Hi";
StringBuffer sb=new StringBuffer(s);
String s1=new String("There");
StringBuffer sb1=new StringBuffer(s1);
if(s==sb){
System.out.println("s==sb");
}if(s.equals(sb)){
System.out.println("s.equals(sb)");
}if(s1.equals(sb1)){
System.out.println("s1.equals(sb1)");
}

Choose the one below:

  1. . It will not compile at if(s==sb) because operands on both side are not compatible
  2. A. It will print s1.equals(sb1)
  3. B. It will print s.equals(sb)
  4. C. It will compile successfully, but it will not output anything

31 . Consider that following code is declared in BussyThread.java file

public class BussyThread extends Thread{
      public void run(){
      for(int i=0;i<10; i++){
      i=i-1;
      }//end of for loop
     }//end of run()
public static void main(String args[]){
BussyThread b1=new BussyThread();
BussyThread b2=new BussyThread();
b1.start();
b2.start();
}
}//end of class

Above code will start two threads b1 and b2. Select True statements for above code?

  1. Only b1 thread will get chance to run
  2. Only b2 thread will get chance to run
  3. Both thread will get chance to run sharing CPU time
  4. Neither of the thread will be able to run.

32 . What changes in run() method of BussyThread will enable both threads to run?

  1. adding yield() into run method
  2. adding try{sleep(1000);}catch (InterruptedException e){} into run method
  3. adding wait(1000) into run method
  4. Neither of the above

33 . Consider the following classes are in MyThread.java, YourThread.java, and Driver.java files:

public class MyThread implements Runnable{
public void run(){
System.out.println("Running MyThread");
}
}//end of MyThread
public class YourThread extends Thread{
public YourThread(Runnable r){
super(r);
}
public void run(){
System.out.println("Running YourThread");
}
}//end of YourThread
public class Driver{

public static void main(String args []){
MyThread t1= new MyThread();
YourThread t2 = new YourThread(t1);
t2.start();
}
}//end of class

If you try to run Driver class what will be result?

  1. It will output “Running MyThread.”
  2. It will output “Running YourThread.”
  3. It will output both “Running MyThread,” and “Running YourThread.”
  4. It will not run.

34 . Consider following code:

String s=null;
String t="null";
 if (s==t)
  {
      System.out.println("s equal to t");
  }else
  {
    System.out.println("s not equal to t");
  }

what will result if you try to compile and run above code?

  1. it compiles successfully, but throws NullpointerException at if (s==t)
  2. It will not compile.
  3. It compiles successfully and output “s equal to t”
  4. It compiles successfully and output “s not equal to t”

35 . Consider the following code:

public void demo(){
String s[];
    if (s.equals(null))
    {
       System.out.println("s is null");
    }else
    {
        System.out.println("s is not equal");
    }
}

What will be result if you try to compile and run above code?

  1. Compile error produced, “variable s may not have been initialized.”
  2. It compile successfully, but throws NullpointerException at if ( s.equals(null) )
  3. It compile successfully, and outputs “s is null.”
  4. It compile successfully, and outputs “s is not null.”

36 . Consider the following code:

public class MyList
{
private static final int MAX_SIZE = 10;
private Object [] list = new Object[MAX_SIZE];
public void add(Object obj)
{
   int size=list.length;
   if(size >= MAX_SIZE)
   {

        class ListExpander
       {
              public void expand()
             {
                    Object temp [] = list;
                   list = new Object[size+MAX_SIZE];
                   for ( i=0;i<temp.length; i++)
                 {
                       list[i]=temp[i];
                 }
           }//end of  public void expand()
     } end of class ListExpander
        ListExpander listEx = new ListExpander();
        listExp.expand();
        list[size] = obj;
  }//end of if
}//end of add
}//end of class MyList

What will be result if you try to compile and run the above code?

  1. Compiler error reported, “Cannot refer inside an inner class to a non-final variable ‘size’ defined in a different method.”
  2. Compiler error reported, “Cannot refer inside an inner class to a private member variable ‘list’ defined in enclosing class MyList.”
  3. Compiler error reported, “Cannot refer inside an inner class to a static member variable MAX_SIZE defined in enclosing class MyList.”
  4. It compiles and runs successfully.

37 . Consider following example of an inner class:

public class MyTest{
public String publicVariable = "a";
private String privateVariable = "b";
public static int SIZE = 0;
private static int MAX_SIZE = 0;
public static DemoHelper{
      public demo{
      System.out.println("Demo = "+XXX);
      }
      }
}//end of inner class
}

which variable of the MyTest class will be able to use in place of XXX?

  1. publicVariable
  2. privateVariable
  3. SIZE
  4. MAX_SIZE

38 . What will be result if you try to compile and run following code?

public class Record extends String{}

Choose the one below:

  1. Compiler error reported, “Can not extend a final class.”
  2. Compiler error reported, “Must implement method int compareTo(Object).”
  3. Compile and run successfully.
  4. None of the above.

39 . Consider the following two classes:

public class Parent{
protected void demo() throws Exception{}
} // end of Parent class
public class Child extends Parent{
private void demo() {}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not reduce the visibility of the inherited method.”
  2. Compiler object for demo() method of a Child class, “Inherited method is not compatible with void Parent.demo() throws Exception.”
  3. Compile successfully.
  4. None of the above

40 . Consider the following two classes:

public class Parent{
protected void demo() {}
} // end of Parent class
public class Child extends Parent{
public void demo() throws Exception{}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
  2. Compiler object for demo() method of a Child class, “Exception java.lang.Exception in throws clause of void Child.demo() is not compatible with void Parent.demo().”
  3. Compile successfully
  4. None of the above

41 . Consider the following two classes:

public class Parent{
protected void demo() {}
} // end of Parent class
public class Child extends Parent{
public int demo()
{return 0;}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
  2. Compiler object for the method of a Child class, “Return type is not compatible with void Parent.demo().”
  3. Compile successfully.
  4. None of the above

42 . Consider the following two classes:

public class Parent{
protected static void demo() {}
} // end of Parent class
public class Child extends Parent{
public void demo() {}
}

What will be result if you try to compile above two classes?

  1. Compiler object for the method of a Child class, “Can not widen the visibility of the inherited method.”
  2. Compiler object for the method of a Child class, “inherited method void Child.demo() is not compatible with void Parent.demo().”
  3. Compiler object for the method of a Child class, “The instance method can not override the static method from Parent.”
  4. Compile successfully.

43 . Consider that class Employee and Salesman are in different file called Employee.java and Salesman.java:

/** Employee.java file*/
public class Employee{
int salary=1000;
public int getSalary(){
return salary;
}
}
/**  End of Employee.java file*/
/** Salesman.java file*/
public class Salesman extends Employee{
int commission =100;
public int getSalary(){
return salary+commission;
}
public static void main(String [] args){
Salesman sm = new Salesman();
Employee em = sm;
System.out.println(em.getSalary());
}
}
/**  End of Salesman.java file*/

What will be result if you try to compile and run above code?

  1. Compiler error reported , “Type mismatch: Cannot convert from Salesman to Employee.”
  2. It compile successfully and outputs 1000.
  3. It compiles successfully and outputs 1100.
  4. None of the above

44 . Considering following code what will be the result if you try to compile the following code:

public abstract class Test{
public void demo(){
System.out.println("demo");
}
}

Choose the one below:

  1. It will compile successfully.
  2. Compiler error reported, “An abstract method must be defined.”
  3. Compiler error reported, “Invalid declaration of class.”
  4. None of the above

45 . Considering following code what will be the result if you try to compile the following code:

public class Test{
public abstract void demo();
}

Choose the one below:

  1. Compiler error reported, “Method requires a body instead of semicolon.”
  2. Compiler error reported, “Abstract methods are only defined by abstract classes.”
  3. Compile successfully.
  4. None of the above.

46 . The GenericList has the following method:

public void addItem(Object item)

You are writing a class GroceryList that extends GenericList. Which of the following would be legal declarations of overloading methods?

Choose the one below:

  1. public void addItem(Vector item)
  2. public void addItem(Object [] items) throws Exception
  3. protected void addItem(Object item)
  4. All of the above

47 . What will be result if you try to compile the following code?

public class Parent{
String name=null;
public Parent(String n){
name=n;
}
}
public class Child extends Parent{
String type="X";
}

Choose the one below:

  1. Compile successfully.
  2. Compiler error reported, because Parent class did not declare constructor with arguments ().
  3. Compiler error reported, because Child class did not declare a constructor.
  4. Both of the above B and C

48 . What will be legal statement in the following method?

public void demo(int x){
XXX y=10;
}

Choose the one below:

  1. public int
  2. int
  3. final int
  4. static int

49 . What will be result if you try to compile and run following code fragement?

public void demo (String [] args){
int i=1;
for(int i=0;i<args.length;i++)
{
        System.out.println(args[i]);
}
}

Choose the one below:

  1. Compile successfully, but throws IndexOutOfBoundException during runtime.
  2. Compile error reported, “Local name i is already defined.”
  3. Throws NullPointerException during runtime
  4. None of the above

Answers

1 : 4 is correct.

2 : 4 is correct.

3 : 1 is correct.

4 : 4 is correct.

5 : 2 & 4 is correct.

6 : 1 & 4 is correct.

7 : 3 & 4 is correct.

8 : 1 is correct.

9 : 2 is correct.

10 : 2 is correct.

11 : 4 is correct.

12 : 3 & 4 is correct.

13 : 3 & 4 is correct.

14 : 2,3,4 & 5 is correct.

15 : 1 is correct.

16 : 4 is correct. This is an example of overriding method.

17 : 4 is correct.

18 : 3 is correct.

19 : 2 is correct.

20 : 4 is correct.

21 : 1,2 & 3 is correct.

22 : 3 & 4 is correct.

23 : 1 & 2 is correct.

24 : 4 is correct.

25 : 1 & 2 is correct.

26 : 3 is correct.

27 : 4 is correct.

28 : 3 is correct.

29 : 3 is correct.

30 : 1 is correct.

31 : 3 is correct.

32 : 1 & 2 is correct.

33 : 2 is correct.

34 : 4 is correct.

35 : 1 is correct.

36 : 1 is correct.

37 : 3 & 4 is correct.

38 : 1 is correct.

39 : 1 is correct.

40 : 2 is correct.

41 : 2 is correct.

42 : 3 is correct.

43 : 3 is correct.

44 : 1 is correct.

45 : 2 is correct.

46 : 1 & 2 is correct.

47 : 2 is correct.

48 : 2 & 3 is correct.

49 : 2 is correct.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better