OCJP Final Set-3
OCJP Final Set-3
6 (CX-310-065 , CX-310-066)
Prepared by : http://www.techfaq360.com
1.public
2.protected
3.private
4.All of the above
Explanation :
D is the correct answer.
constructor declarations may include the access modifiers public, protected, or private
Question - 2
If you run the code below, what gets printed out?
1.Bic
2.icy
3.ic
4.error: no method matching substring(int,char)
Explanation :
C is the correct answer.
char iEnd=3; will convert to ASCII value 3. if you declare char iEnd='a'; then it
converts to 97 ( ASCII value of 'a' is 97)
Question - 3
True of False.
Using the instanceof operator on an interface will cause a runtime
exception.
1.True
2.False
3.None of the above
4.None of the above
Explanation :
B is the correct answer.
The instanceof operator can be used as follows: - on classes to see if a particular class
is derived from another (ie, is an instance of) - on interfaces to see if a class
implements a particular interface
Question - 4
Which of the following pieces of code compiles without any errors?
Explanation :
B is the correct answer.
Question - 5
True or False.
The StringBuffer class does not have a concat() method.
1.True
2.False
3.None of the above
4.None of the above
Explanation :
A is the correct answer.
Question - 6
Which of the following methods are defined in the Object class?
1.toString()
2.equals(Object o)
3.wait()
4.All of the above
Explanation :
D is the correct answer.
Question - 7
True or False.
The StringBuffer class inherits from the String class.
1.True
2.False
3.None of the above
4.None of the above
Explanation :
B is the correct answer.
Question - 8
private class B {
Explanation :
B is the correct answer.
Question - 9
public class Point {
int x = 1, y = 1;
abstract void alert();
}
Explanation :
B is the correct answer.
If there is any abstract method in a class then the class should be abstract.
Question - 10
abstract class Point {
int x = 1, y = 1;
abstract void alert();
}
public class A{
public static void main(String[] args){
Point p = new Point();
}
}
What is the output ?
Question - 11
Which of the below statement is true?
1.A subclass of an abstract class that is not itself abstract may be instantiated.
2.abstract class can't be instantiated.
3.abstract class can be instantiated.
4.None of the above
Explanation :
A and B is the correct answer.
Question - 12
Which of the following lines will print false?
1.Lines 10 and 12
2.Line 12 only
3.Line 8 and 10
4.None of these
Explanation :
D is the correct answer.
Question - 13
What is the correct ordering for the import, class and package
declarations when found in a Java class?
Explanation :
A is the correct answer.
Question - 14
What will be the result of compiling the following code:
Explanation :
B is the correct answer.
static variables are initiatized automatically. default initialization value for int is 0 and
boolean is false.
Question - 15
Which of the following is correct?
1.String temp [] = new String {"j" "a" "z"};
2.String temp [] = { "j " " b" "c"};
3.String temp = {"a", "b", "c"};
4.String temp [] = {"a", "b", "c"};
Explanation :
D is the correct answer.
Question - 16
What is the correct declaration of an abstract method that is
intended to be public?
Explanation :
A is the correct answer.
Question - 17
Under what situations do you obtain a default constructor?
Explanation :
B is the correct answer.
Question - 18
Which of the following can be used to define a constructor for this
class, given the following code:
Explanation :
A is the correct answer.
Constructor should not have any return type and should not be static.
Question - 19
Assuming a method contains code which may raise an Exception (but not
a RuntimeException),
what is the correct way for a method to indicate that it expects the
caller to handle that exception?
1.throw Exception
2.throws Exception
3.new Exception
4.Don't need to specify anything
Explanation :
B is the correct answer.
throws Exception from metthod and the caller method should catch to handle the
exception.
Question - 20
Which of the following is a legal return type of a method overloading
the following method ?
1.void
2.int
3.String
4.Can be anything
Explanation :
D is the correct answer.
Explanation :
A is the correct answer.
In case of overriding , super class method and the sub class method should have same
return type. if super class method return ArrayList then subclass overriding method
should return same or sub type of ArrayList. An overriding method can also return a
subtype of the type returned by the overridden method. This is called a covariant
return type.
Question - 22
What is the value of y?
int y = 2 % 4;
1.0
2.2
3.4
4.Compile Error
Explanation :
B is the correct answer.
x % y is x if x is less than y.
Question - 23
What is the output for the below code ?
1.10
2.Compile with error
3.0
4.Compile successfully but Runtime Exception
Explanation :
B is the correct answer.
Question - 24
class A {
B b = new B();
C c = (C) b;
}
Referring to the above, when is the cast of "b" to class C allowed?
Explanation :
C is the correct answer.
Question - 25
Is this legal
public class Test {
static { int a = 5; }
public static void main(String[] args){
System.out.println(a);
}
}
1.Yes
2.No
3.Can't Say
4.None
Explanation :
B is the correct answer.
A variable declared in a static initialiser is not accessible outside its enclosing block.
Question - 26
Which variables can an inner class access from the class which
encapsulates it?
Explanation :
A is the correct answer.
inner class can access static , final and instance variables of outer class.
Question - 27
What is the output ?
public class Test {
final int k;
public static void main(String[] args){
System.out.println(k+1);
}
}
1.0
2.1
3.Compile Error.
4.None of the above.
Explanation :
C is the correct answer.
Question - 28
Inner class can extend _______ .
Explanation :
C is the correct answer.
Question - 29
Which one of the following is a limitation of subclassing the Thread
class?
Explanation :
D is the correct answer.
Java don't support multiple inheritance.Subclassing the Thread Class , you can't
extend any other class.
Question - 30
What is the effect of issuing a wait() method on an object ?
1.If a notify() method has already been sent to that object then it has no effect
2.The object issuing the call to wait() will halt until another object sends a notify() or
notifyAll() method
3.An exception will be raised
4.The object issuing the call to wait() will be automatically synchronized with any
other objects using the receiving object.
Explanation :
B is the correct answer.
issuing the call to wait() will halt until another object sends a notify() or notifyAll()
method.
Question - 31
What is the effect of adding the sixth element to a vector created in
the following manner?
new Vector(5, 10);
Explanation :
C is the correct answer.
Question - 32
Observe the code below
public class Test {
int a[] = new int[4];
void aMethod()
{
int b = 0 , index;
a[a[b]] = a[b] = b = 2;
index = ??? ;
System.out.println(a[index]);
}
}
What value assigned to the variable index will print a non zero
result?
1.0
2.1
3.2
4.3
Explanation :
A is the correct answer.
the operands are evaluated from left to right here so in the expression a[a[b]] the value
of b is 0 and a[0] also holds the value 0 when the expression is evaluated so the array
element at index 0 is assigned the value 2.
Question - 33
What is the output of the bellow code?
Object a = "hello";
String b = "hello";
if(a == b)
System.out.println("equal");
else
System.out.println("not equal");
1.equal
2.not equal
3.Code not compile
4.None of the above
Explanation :
A is the correct answer.
It does not matter if the object reference is of type Object this will still return true
because both of them point to the same String object in the string pool.
Question - 34
Is the bellow statement True or False?
The garbage collector is required to makes sure that all objects
held by soft references are garbage collected before the VM
throws
an OutOfMemoryError.
1.True
2.False
3.Sometime true , sometime false.
4.None of the above.
Explanation :
A is the correct answer.
If the VM finds itself unable to allocate memory for a new object it is required to kick
start the garbage collector and reclaim all objects that are not held by strong
references before throwing an OutOfMemoryError.
Question - 35
public class A{
private void test1(){
System.out.println("test1 A");
}
}
System.out.println("test1 B");
}
}
}
}
1.test1 A
2.test1 B
3.Not complile because test1() method in class A is not visible.
4.None of the above.
Explanation :
C is the correct answer.
Not complile because test1() method in class A is not private so it is not visible.
Question - 36
public class A{
private void test1(){
System.out.println("test1 A");
}
}
System.out.println("test1 B");
}
}
}
}
1.test1 B
2.test1 A
3.Not complile because test1() method in class A is not visible
4.None of the above
Explanation :
A is the correct answer.
This is not related to superclass , B class object calls its own method so it compile and
output normally.
Question - 37
public class A{
public void test1(){
System.out.println("test1 A");
}
}
System.out.println("test1 B");
}
}
}
}
1.test1 B
2.test1 A
3.Not complile because test1() method in class A is not visible
4.None of the above
Explanation :
A is the correct answer.
Question - 38
class c1
{
public static void main(String a[])
{
c1 ob1=new c1();
Object ob2=ob1;
System.out.println(ob2 instanceof Object);
System.out.println(ob2 instanceof c1);
}
}
1.Prints true,false
2.Print false,true
3.Prints true,true
4.compile time error
Explanation :
C is the correct answer.
Question - 39
class C{
static int f1(int i) {
System.out.print(i + ",");
return 0;
}
public static void main (String[] args) {
int i = 0;
i = i++ + f1(i);
System.out.print(i);
}
}
1.Prints: 0,0
2.Prints: 1,0
3.Prints: 0,1
4.Compile-time error
Explanation :
B is the correct answer.
trace it by yourself.
Question - 40
public class C {
public C(){
System.out.println("tt");
}
Explanation :
B is the correct answer.
C c = new D(); NOT COMPILE because D don't have default constructor. If super
class has different constructor other then default then in the sub class you can't use
default constructor
Question - 41
public class C {
public C(){
System.out.println("tt");
}
System.out.println("tt");
}
Explanation :
A is the correct answer.
C c = new D(8); COMPILE because D don't have default constructor. If super class
has different constructor other then default then in the sub class you can't use default
constructor
Question - 42
public class A {
{ System.out.println("block");}
public A(){
System.out.println("A");
}
1.A block
2.block A
3.A
4.None of the above
Explanation :
B is the correct answer.
Question - 43
public class A {
static{System.out.println("static");}
{ System.out.println("block");}
public A(){
System.out.println("A");
}
Explanation :
B is the correct answer.
Question - 44
class Bird {
int i;
boolean b;
float f;
public Bird() {
System.out.println(i);
System.out.println(b);
System.out.println(f);
}
}
Explanation :
A is the correct answer.
Question - 45
public class Test {
public static void main(String[] args){
StringBuffer a = new StringBuffer("Hello");
StringBuffer b = a.append("World");
System.out.println(b);
1.World
2.HelloWorld
3.World
4.None of the above
Explanation :
B is the correct answer.
append
Question - 46
class c1
{
static{
System.out.println("static");
}
public static void main(String a[])
{
System.out.println("main");
}
}
Explanation :
A is the correct answer.
Question - 47
When a byte is added to a char, what is the type of the result?
1.byte
2.int
3.long
4.non of the above
Explanation :
B is the correct answer.
The result of all arithmetic performed with the binary operators (not the assignment
operators) is an int, a long, a float, or a double. Here byte and char are promoted to
int, so the result is an int.
Question - 48
Which of the following statements are true?
1.1 and 4
2.2 and 4
3.2 and 3
4.3 and 4
Explanation :
B is the correct answer.
If a program keeps creating new references without any being discarded it may run
out of memory. Unlike most aspects of Java garbage collection is platform dependent
Question - 49
Is the bellow import is correct?
Explanation :
A is the correct answer.
The static import construct allows unqualified access to static members without
inheriting from the type containing the static members. import static java.lang.Math.*;
can be use directly double r = cos(PI * 3); no need Math.PI
Question - 50
What is the output ?
package bean;
public class Abc {
public static int index_val = 10;
}
package com;
import static bean.Abc.index_val;
1.10
2.compile error, index_val not defined
3.Compile error at import static bean.Abc.index_val;
4.None of the error
Explanation :
A is the correct answer.
The static import construct allows unqualified access to static members without
inheriting from the type containing the static members. J2SE 5.0 or later allows static
import like import static bean.Abc.index_val; and can be use directly
System.out.println(index_val);
Question - 51
What will happen when you attempt to compile and run the following
code
Explanation :
B is the correct answer.
1.A compile time error indicating that no run method is defined for the Thread class
2.A run time error indicating that no run method is defined for the Thread class
3.Clean compile and at run time the values 0 to 9 are printed out
4.Clean compile but no output at runtime
Explanation :
D is the correct answer.
This is a bit of a sneaky one as I have swapped around the names of the methods you
need to define and call when running a thread. If the for loop were defined in a
method called public void run() and the call in the main method had been to b.start()
The list of values from 0 to 9 would have been output
Question - 53
What will happen when you attempt to compile and run the following
code
class Base{
protected int i = 99;
}
public class Ab{
private int i=1;
public static void main(String argv[]){
Ab a = new Ab();
a.hallow();
}
Explanation :
A is the correct answer.
Abstract and native methods can't have a body: void hallow() abstract void hallow()
Question - 54
What is the output?
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
1.Compile error
2.Runtime Exception
3.7 9
4.None of the above
Explanation :
B is the correct answer.
| Without generics, the compiler does not know what type is appropriate for this
TreeSet, so it allows everything to compile. But at runtime he TreeSet will try to sort
the elements as they are added, and when it tries to compare an Integer with a String it
will throw a ClassCastException.
| Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot
be cast to java.lang.Integer.
Question - 55
What is the output for the bellow code ?
import java.util.EnumSet;
import static java.lang.System.out;
Explanation :
A is the correct answer.
EnumSet.range(from, to) : Creates an enum set initially containing all of the elements
in the range defined by the two specified endpoints
Question - 56
Is the bellow statement is true ?
1.true
2.false
3.Can't say
4.None of the above
Explanation :
A is the correct answer.
Pattern p = Pattern.compile("a{1,3}b?c*");
Matcher m = p.matcher("aaab");
boolean b = m.matches();
System.out.println(b);
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
X? X, once or not at all X* X, zero or more times X+ X, one or more times X{n} X,
exactly n times X{n,} X, at least n times X{n,m} X, at least n but not more than m
times
Question - 58
Fill the gap:
1. class Outer {
2. class Inner{ }
3. }
4. class Test {
5. public static void main(String[] args) {
6. Outer o = new Outer();
7.
8. }
9. }
Explanation :
B is the correct answer.
Compile error : Cannot instantiate the type Calendar. In order to create a Calendar
instance, you have to use one of the overloaded getInstance() static factory methods:
Calendar cal = Calendar.getInstance();
Question - 60
public class A {}
}catch(Exception e){
e.printStackTrace();
}
1.Compilation Fail
2.java.io.NotSerializableException: Because class A is not Serializable.
3.No Exception
4.None of the above
Explanation :
B is the correct answer.