Final Set-5
Final Set-5
6 (CX-310-065 , CX-310-066)
Prepared by : http://www.techfaq360.com
else
{
System.out.println("Not equal! " + Output);
}
Explanation :
D is the correct answer.
(b1 == true) is not true so the statement ((Output += 10) == 20) will not execute.
because if first statement is true then only 2nd statement will execute in case of
and(&&) condition.
Question - 2
Which of the following statements are correct?
1.1 and 4
2.1,2 and 3
3.2 and 4
4.2 and 3
Explanation :
C is the correct answer.
If multiple listeners are added to a component the events will be processed for all but
with no guarantee in the order
Question - 3
Which of the following modifiers can be applied to a class that is
not a nested class?
1.public
2.protected
3.private
4.static
Explanation :
A is the correct answer.
Question - 4
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();
}
}
Given the above classes which of the following code will compile
without error?
1)o1=o2;
2)b=ob;
3)ob=b;
4)o1=b;
1.1,2,4
2.2,3,4
3.1,3,4
4.3,4
Explanation :
A is the correct answer.
1)o1=o2; 2)b=ob; 4)o1=b; are true because you can A Superclass Variable Can
Reference a Subclass Object.
Question - 5
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}
1.1
2.2
3.3
4.0
Explanation :
A is the correct answer.
Main thread is a thread and calling run methods will not create thread.
Question - 6
What will happen when you attempt to compile and run the following
code?
class Base
{
int i = 99;
1.Derived.amethod() -1 Derived.amethod()
2.Derived.amethod() 99
3.Derived.amethod() 99
4.Derived.amethod()
Explanation :
A is the correct answer.
Question - 7
Which data type is wider for the purpose of casting: float or long?
1.float
2.long
3.none of the above
4.none of the above
Explanation :
A is the correct answer.
float is wider than long, because the entire range of long fits within the range of float.
Question - 8
Which statement is correct ?
1.If super class has different constructor other then default then in the sub class you
can use default constructor
2.If super class has different constructor other then default then in the sub class you
can't use default constructor
3.There is no relation between superclass and subclass.
4.none of the above
Explanation :
B is the correct answer.
If super class has different constructor other then default then in the sub class you
can't use default constructor.
Question - 9
import java.util.*;
class C
{
final Vector v;
C()
{
v=new Vector();
}
C(int i)
{
Explanation :
A is the correct answer.
The blank final field v may not have been initialized. If you do C c= new C(5) then
final variable will be not initialized , So this is giving compile error.
Question - 10
? Each element must be unique
? Duplicate elements must not replace old elements.
? Elements are not key/value pairs.
? Accessing an element can be almost as fast as performing a similar
operation on an array.
Explanation :
D is the correct answer.
All the mentioned features are related to HashSet. ? Each element must be unique ?
Duplicate elements must not replace old elements. ? Elements are not key/value pairs.
? Accessing an element can be almost as fast as performing a similar operation on an
array.
Question - 11
What is the output for the below code ?
class Test {
public static void main (String[] a1) {
System.out.println(a1.length()); //1
System.out.println(a1[0]); //2
System.out.println(a1); //3
}}
Explanation :
A is the correct answer.
Question - 12
Given the code below, and making no other changes, which access
modifiers
(public, protected or private) can legally be placed before
myMethod() on line 3?
If line 3 is left as it is, which keywords can legally be placed
before myMethod
on line 8?
1. class A
2. {
3. void myMethod() {}
4. }
5.
6. class B extends A
7. {
8. void myMethod() {}
9. }
Explanation :
A is the correct answer.
Methods may not be overridden to be more private. For example, a protected method
can't be overridden by a default one. But you can override a default method to make it
protected or public. .
Question - 13
What will be output by the following line of code?
System.out.println(010|4);
1.12
2.10
3.11
4.14
Explanation :
A is the correct answer.
No explanation available
Question - 14
In the following pieces of code, A and D will compile without any
error. True/False?
1.true
2.false
3.none of these
4.no idea
Explanation :
B is the correct answer.
Question - 15
What is the output ?
class Test
{
public static void main(String a[])
{
Boolean b = new Boolean("abcd");
System.out.println(b.booleanValue());
}
}
1.true
2.false
3.compile with error.
4.Runtime Exception
Explanation :
B is the correct answer.
Question - 16
Which of the following classes will not allow unsynchronized read
operations by multiple threads?
1.Vector
2.TreeMap
3.TreeSet
4.HashMap
Explanation :
A is the correct answer.
}
}
Explanation :
A is the correct answer.
If super class has constructor other than no-arg constructor then subclass can't use no-
arg constructor. Either define a no-arg constructor in base, or call super(n) as the first
statement in your constructors in Super.
Question - 18
What will happen if you attempt to compile and run the following
code?
}
}
1.19 followed by 20
2.19 followed by 11
3.Compile time error
4.10 followed by 1
Explanation :
B is the correct answer.
From jdk 1.5 onwards : Unboxing conversion converts objects of wrapper types to
values of corresponding primitive types.
Question - 19
What will happen when you attempt to compile and run the following
code with the command line "hello there"
Explanation :
A is the correct answer.
Question - 20
What will happen when you attempt to compile and run the following
code
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}
1.Compile with error, Base is a keyword
2.Compile successfully and no output at runtime
3.Print Base
4.Runtime error Base has no valid constructor
Explanation :
B is the correct answer.
Because the method in Base called Base has a return type it is not a constructor and
there for does not get called on creation of an instance of its child class In
Question - 21
class c1
{
public static void main(String a[])
{
System.out.println(Double.NaN==Double.NaN);
System.out.println(Double.NaN!=Double.NaN);
System.out.println(Float.NaN==Double.NaN);
}
}
Explanation :
A is the correct answer.
Comparison of a double with double.Nan (Not a Number) will always return false.
Question - 22
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 - 23
Which of the following statements are true?
1.1
2.2
3.3
4.4
Explanation :
D is the correct answer.
No explanation available
Question - 24
What will happen when you attempt to compile and run this code?
private class Base{}
public class Vis{
transient int iVal;
public static void main(String elephant[]){
}
}
Explanation :
A is the correct answer.
outer class can't be private , only public, abstract and final allowed.
Question - 25
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}
1.Prints nothing
2.Prints: 1
3.Prints: 01
4.Compile-time error
Explanation :
B is the correct answer.
a.run() method was called instead of a.start(); so the full program runs as a single
thread so a.run() is guaranteed to complete.
Question - 26
class Test
{
static int s;
public static void main(String a[]){
Test obj=new Test();
obj.m1();
System.out.println(s);
}
void m1()
{
int x=1;
m2(x);
System.out.println(x+"");
}
void m2(int x){
x=x*2;
s=x;
}
}
1.prints 1,2
2.prints 2,0
3.prints 2,2
4.compile time error
Explanation :
A is the correct answer.
Only objects and arrays are passed by reference.other are passed by value.s is a static
variable which is global to the class.
Question - 27
What happens if a class defines a method with the same name and
parameters as a method in its superclass?
Explanation :
B is the correct answer.
No explanation available
Question - 28
Fill in all the gaps
expr = 1;
switch (expr) {
case 1:
System.out.println("ONE"); break;
case 2:
System.out.println("TWO"); break;
case 3:
System.out.println("THREE"); break;
default:
assert false;
}
Question - 29
What is the output ?
Integer i = null;
int j = i;
System.out.println(j);
}
1.0
2.null
3.Compile with error
4.java.lang.NullPointerException
Explanation :
D is the correct answer.
Question - 30
What is the output of the bellow code ?
doSomething(1);
doSomething(2.0);
Explanation :
A is the correct answer.
The method resolution inludes three passes: 1. Attempt to locate the correct method
without any boxing, unboxing, or vararg invocations. 2. Attempt to locate the correct
method with boxing, unboxing, and without any vararg invocations. 3. Attempt to
locate the correct method with boxing, unboxing, or vararg invocations.
Question - 31
What is the output ?
Explanation :
A is the correct answer.
The method resolution inludes three passes: 1. Attempt to locate the correct method
without any boxing, unboxing, or vararg invocations. 2. Attempt to locate the correct
method with boxing, unboxing, and without any vararg invocations. 3. Attempt to
locate the correct method with boxing, unboxing, or vararg invocations.
Question - 32
What is the output?
1.c1 == c2 : true
2.c1 == c2 : false
3.Compile with error
4.None of the above
Explanation :
A is the correct answer.
No Exp
Question - 33
public class SuperClass {
public ArrayList doIt(){
ArrayList lst = new ArrayList();
System.out.println("super doIt()");
return lst;
}
sb.doIt();
1.subclas doIt()
2.super doIt()
3.Compile with error
4.None of the above
Explanation :
C is the correct answer.
Question - 34
public class SuperClass {
public List doIt(){
List lst = new ArrayList();
System.out.println("super doIt()");
return lst;
}
}
public class SubClass extends SuperClass{
sb.doIt();
1.subclas doIt()
2.super doIt()
3.Compile with error
4.None of the above
Explanation :
A is the correct answer.
subtype return is eligible in jdk 1.5 for overidden method but not super type return.
super class method return List so subclass overriden method should return same or
sub type of List.
Question - 35
class C{
int i;
public static void main (String[] args) {
int i; //1
private int a = 1; //2
protected int b = 1; //3
public int c = 1; //4
System.out.println(a+b+c); //5
}}
Explanation :
B is the correct answer.
The access modifiers public, protected and private, can not be applied to variables
declared inside methods.
Question - 36
Which variables can an inner class access from the class which
encapsulates it.
Explanation :
D is the correct answer.
Inner classes have access to all variables declared within the encapsulating class.
Question - 37
class c1
{
public void m1()
{
System.out.println("m1 method in C1 class");
}
}
class c2
{
public c1 m1()
{
return new c1(){
public void m1()
{
System.out.println("m1 mehod in anonymous class");
}};}
public static void main(String a[])
{
Explanation :
B is the correct answer.
the anonymous class overrides the m1 method in c1.so according to the dynamic
dispatch the m1 method in the anonymous is called
Question - 38
What is the output?
import java.util.Iterator;
import java.util.TreeSet;
Iterator it = s1.iterator();
while (it.hasNext() ) {
System.out.print( it.next() + " " );
}
Explanation :
A is the correct answer.
B TreeSet assures no duplicate entries.it will return elements in natural order, which,
for Strings means alphabetical.
Question - 39
In the bellow code ,
1.Collections.sort(alist);
2.Collection.sort(alist);
3.alist.sort();
4.None of the above
Explanation :
A is the correct answer.
Question - 40
In the bellow code ,
1.Collections.shuffle(alist);
2.Collection.shuffle(alist);
3.alist.shuffle();
4.None of the above
Explanation :
A is the correct answer.
Question - 41
How can we create thread safe List ?
1.Collections.synchronizedList(List list);
2.Collection.synchronizedList(List list);
3.List.synchronizedList();
4.None of the above
Explanation :
A is the correct answer.
Question - 42
What is the output ?
1.5 4 3 2 1 0
2.Comiple time error , Queue not valid Java Interface
3.Runtime Exception
4.None of the above
Explanation :
A is the correct answer.
Question - 43
Which statement is true ?
Explanation :
D is the correct answer.
A priority queue does not permit null elements. A priority queue does not permit
insertion of non-comparable objects. A priority queue orders elements according to an
order specified at construction time
Question - 44
What is the Difference between remove() ans poll() method of Queue?
1.remove() differs from poll() only in that it throws an exception if this queue is
empty.
2.remove() differs from poll() only in that it null if this queue is empty.
3.Both are true
4.None of the above
Explanation :
A is the correct answer.
remove(): Retrieves and removes the head of this queue. This method differs from
poll only in that it throws an exception if this queue is empty. poll(): Retrieves and
removes the head of this queue, or returns null if this queue is empty.
Question - 45
A collection designed for holding elements prior to processing
__________?
1.Queue
2.ArrayList
3.Map
4.Vector
Explanation :
A is the correct answer.
Queue : A collection designed for holding elements prior to processing. Besides basic
Collection operations, queues provide additional insertion, extraction, and inspection
operations.
Question - 46
What is true about Deque?
1.A linear collection that supports element insertion and removal at both ends
2.A linear collection that supports element insertion and removal at one end
3.The name deque is short for "double ended queue"
4.1 and 3
Explanation :
D is the correct answer.
The name deque is short for "double ended queue" .A linear collection that supports
element insertion and removal at both ends
Question - 47
What is true about Map ?
A Map is an object that maps keys to values. A map cannot contain duplicate keys:
Question - 48
What is the output of the bellow code, if we run using command line
import java.util.*;
command line :
java Freq if it is to be it is up to me to delegate
1.8 distinct words: {to=3, delegate=1, be=1, it=2, up=1, if=1, me=1, is=2}
2.7 distinct words: {delegate=1, be=1, it=2, up=1, if=1, me=1, is=2}
3.6 distinct words: {to=3, delegate=1, be=1, it=2, up=1, is=2}
4.None of the above
Explanation :
A is the correct answer.
Question - 49
What will be the result of compiling the following code:
Explanation :
D is the correct answer.
Question - 50
When the application Thrown NullPointerException ?
Explanation :
D is the correct answer.
Question - 51
What will be the result of compiling the following code:
Explanation :
B is the correct answer.
Question - 52
public class A implements Serializable {
transient int a = 7;
static int b = 9;
}
What is the output?
1.9 0 9
2.9 7 9
3.Runtime Exception
4.Compile with error
Explanation :
A is the correct answer.
static and transient variables are not serialized when an object is serialized.
Question - 53
Which is the correct way of Instantiate BufferedWriter object?
Explanation :
B is the correct answer.
Question - 54
What will be the result of compiling and run the following code:
}
}
1.true
2.false
3.Compile error
4.None of the above
Explanation :
B is the correct answer.
equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.
Question - 55
What will be the result of compiling and run the following code:
1.true
2.false
3.Compile error
4.None of the above
Explanation :
A is the correct answer.
equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.
Question - 56
When comparing java.io.BufferedWriter and java.io.FileWriter, which
capability exist as a method in only one of two ?
Explanation :
D is the correct answer.
Question - 57
What will be the result of compiling and run the following code:
public class Test {
Explanation :
B is the correct answer.
creating a new instance of the class File, you're not yet making an actual file, you're
just creating a filename
Question - 58
Modifiers Applied to Inner Classes is ________________?
1.private
2.final
3.public
4.All of the above
Explanation :
D is the correct answer.
Modifiers Applied to Inner Classes is final abstract public private protected static
strictfp
Question - 59
class Outer {
private String x = "Outer variable";
void doStuff() {
String z = "local variable";
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
}
}
}
}
What is the output?
1.Compile Fail : local variable z is can't access from within inner class
2.Runtime Exception
3.Compile without error.
4.None of the above
Explanation :
A is the correct answer.
the inner class object cannot use the local variables of the method the inner class is in.
only final can be accessible.
Question - 60
class Outer {
private String x = "Outer variable";
void doStuff() {
final String z = "local variable";
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
}
}
Inner mi = new Inner();
mi.seeOuter();
}
What is the output?
1.Compile Fail : local variable z is can't access from within inner class
2.Outer x is Outer variable Local variable z is local variable
3.Outer x is Outer variable
4.None of the above
Explanation :
B is the correct answer.
the inner class object cannot use the local variables of the method the inner class is in.
only final can be accessible.