1.
Consider the code below & select the correct
ouput from the options:
public class Test {
public static void main(String [] args) {
int x = 5;
boolean b1 = true;
boolean b2 = false;
if ((x == 4) && !b2 )
System.out.print("1 ");
System.out.print("2 ");
if ((b2 = true) && b1 )
System.out.print("3 "); }
2 3
1 3
2
3
Correct Answer
A. Option 1
2.
Which three are legal array declarations?
(Choose THREE)
int []
myScores [];
char []
myChars;
int [6]
myScores;
Dog myDogs
[];
Correct Answer(s)
A. Option 1
B. Option 2
D. Option 4
3.
Consider the given code and select the
correct output:
class Test{
public static void main(String[] args){
int num1 = 012;
int num2 = 0x110;
int sum =num1+=num2;
System.out.println("Ans = "+sum); }}
26
282
Compiles but
error at run
time
Compilation
error
Correct Answer
B. Option 2
4.
Say that class Rodent has a child class Rat
and another child class Mouse. Class Mouse
has a child class PocketMouse. Examine the
following
Rodent rod;
Rat rat = new Rat();
Mouse mos = new Mouse();
PocketMouse pkt = new PocketMouse();
Which one of the following will cause a
compiler error?
rod = mos
pkt = rat
pkt = null
rod = rat
Correct Answer
B. Option 2
5.
Consider the code below & select the correct
ouput from the options:
class Test{
public static void main(String[] args) {
parse("Four"); }
static void parse(String s){
try {
double d=Double.parseDouble(s);
}catch(NumberFormatException nfe){
d=0.0; }finally{
System.out.println(d); } }}
0
Compilation
error
A
ParseExcepti
on is thrown
by the parse
method at
runtime
A
NumberForm
atException
is thrown by
the parse
method at
runtime
Correct Answer
B. Option 2
6.
Consider the code below & select the correct
ouput from the options:
class A{
public int a=7;
public void add(){
this.a+=2; System.out.print("a"); }}
public class Test extends A{
public int a=2;
public void add(){
this.a+=2; System.out.print("t"); }
public static void main(String[] args) {
A a =new Test();
a.add();
System.out.print(a.a); }}
t 7
t 9
a 9
Compilation
error
Correct Answer
A. Option 1
7.
What will be the output of the program?
public class CommandArgsTwo
{
public static void main(String [] argh)
{
int x;
x = argh.length;
for (int y = 1; y <= x; y++)
{
System.out.print(" " + argh[y]);
}
}
}
and the command-line invocation is
> java CommandArgsTwo 1 2 3
0 1 2
2 3
0 0 0
An exception
is thrown at
runtime
Correct Answer
D. Option 4
8.
What will be the result of the following
program?
public class Init {
String title;
boolean published;
static int total;
static double maxPrice;
public static void main(String[] args) {
Init initMe = new Init();
double price;
if (true)
price = 100.00;
System.out.println("|" + initMe.title + "|" +
initMe.published + "|" +
Init.total + "|" + Init.maxPrice + "|" + price+ "|");
}
}
The program
will compile,
and print
|null|false|0|0.
0|0.0|, when
run
The program
will compile,
and print
|null|true|0|0.
0|100.0|,
when run
The program
will compile,
and print |
|false|0|0.0|0.
0|, when run
The program
will compile,
and print
|null|false|0|0.
0|100.0|,
when run
Correct Answer
D. Option 4
9.
Here is the general syntax for method
definition:
accessModifier returnType methodName(
parameterList )
{
Java statements
return returnValue;
}
What is true for the returnType and the
returnValue?
The
returnValue
must be
exactly the
same type as
the
returnType
The
returnValue
can be any
type, but will
be
automatically
converted to
returnType
when the
method
returns to the
caller.
If the
returnType is
void then the
returnValue
can be any
type
The
returnValue
must be the
same type as
the
returnType,
or be of a
type that can
be converted
to returnType
without loss
of
information.
Correct Answer
D. Option 4
10.
Consider the following code and choose the
correct option:
class Test{
class A{ static int x=3; }
static void display(){
System.out.println(A.x); }
public static void main(String[] args) {
display(); }}
3
Compilation
error
Compiles but
error at run
time
0
Correct Answer
B. Option 2
11.
Which of the following lines of code will
compile without warning or error?
1) float f=1.3;
2) char c="a";
3) byte b=257;
4) boolean b=null;
5) int i=10;
Line 3
Line 1, Line
3, Line 5
Line 1, Line 5
Line 5
Correct Answer
D. Option 4
12.
Consider the following code and choose the
correct option:
class Test{
interface Y{
void display(); }
public static void main(String[] args) {
new Y(){
public void display(){
System.out.println("Hello World"); }
}.display(); }}
Hello World
Compilation
error
Compiles but
error at run
time
Compiles but
run without
output
Correct Answer
A. Option 1
13.
Consider the following code and choose the
correct option:
class Test{
static class A{
interface X{
int z=4; } }
static void display(){
System.out.println(A.X.z); }
public static void main(String[] args) {
display(); }}
4
Compilation
error
Compiles but
error at run
time
Correct Answer
A. Option 1
14.
What is the output of the following program?
public class MyClass
{
public static void main( String[] args )
{
private static final int value =9;
float total;
total = value + value / 2;
System.out.println( total );
}
}
0
13.5
13
Compilation
Error
Correct Answer
D. Option 4
15.
Which of the given options is similar to the
following code:
value += sum++ ;
value = value
+ sum; sum =
sum + 1;
sum = sum +
1; value =
value + sum;
value = value
+ sum;
value = value
+ ++sum;
Correct Answer
A. Option 1
16.
What will happen if you attempt to compile
and run the following code?
Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);
19 followed
by 11
19 follwed by
20
Compile time
error
10 followed
by 1
Correct Answer
A. Option 1
17.
Identify the statements that are correct:
(A) int a = 13, a>>2 = 3
(B) int b = -8, b>>1 = -4
(C) int a = 13, a>>>2 = 3
(D) int b = -8, b>>>1 = -4
(A), (B) & (C)
(A), (B), (C) &(D)
(C) & (D)
(A) & (B)
Correct Answer
A. Option 1
18.
Consider the following code:
int x, y, z;
y = 1;
z = 5;
x = 0 - (++y) + z++;
After execution of this, what will be the values
of x, y and z?
x = -7, y = 1,
z = 5
x = 3, y = 2, z
= 6
x = 4, y = 1, z
= 5
x = 4, y = 2, z
= 6
Correct Answer
B. Option 2
19.
Here is the general syntax for method
definition:
accessModifier returnType methodName(
parameterList )
{
Java statements
return returnValue;
}
What is true for the accessModifier?
It must
always be
private or
public
It can be
omitted, but if
not omitted
there are
several
choices,
including
private and
public
The access
modifier must
agree with
the type of
the return
value
It can be
omitted, but if
not omitted it
must be
private or
public
Correct Answer
B. Option 2
20.
What will be the output of the program?
public class CommandArgs
{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is
> java CommandArgs 1 2 3 4
args[2] = 2
args[2] = 3
args[2] = null
An exception
is thrown at
runtime
Correct Answer
D. Option 4
21.
Consider the following code snippet:
int i = 10;
int n = ++i%5;
What are the values of i and n after the code
is executed?
10, 1
11, 1
10,0
11,0
Correct Answer
B. Option 2
22.
Which will legally declare, construct, and
initialize an array?
int [] myList =
{"1", "2", "3"};
int [] myList =
(5, 8, 2);
int myList [] []
= {4,9,7,0};
int myList [] =
{4, 3, 7};
Correct Answer
D. Option 4
23.
Consider the code below & select the correct
ouput from the options:
public class Test {
public static void main(String[] args) {
int x=5;
Test t=new Test();
t.disp(x);
System.out.println("main X="+x);
}
void disp(int x) {
System.out.println("disp X = "+x++);
}}
disp X = 6
main X=6
disp X = 5
main X=5
disp X = 5
main X=6
Compilation
error
Correct Answer
B. Option 2
24.
How many objects and reference variables are
created by the following lines of code?
Employee emp1, emp2;
emp1 = new Employee() ;
Employee emp3 = new Employee() ;
Two objects
and three
reference
variables.
Three objects
and two
reference
variables
Four objects
and two
reference
variables
Two objects
and two
reference
variables.
Correct Answer
A. Option 1
25.
The purpose of the method overriding is to
perform different operation, though input
remains the same.
B) one of the important Object Oriented
principle is the code reusability that can be
achieved using abstraction
Only A is
TRUE
Only B is
True
Both A and B
is True
Both A and B
is FALSE
Correct Answer
A. Option 1
26.
Class Test{
public static void main(String[] args){
byte b=(byte) (45 << 1);
b+=4;
System.out.println(b); }}
What should be the output for the code written
above?
48
94
Compiles but
error at run
time
Compilation
error
Correct Answer
B. Option 2
27.
Which one do you like?What is the value of y when the code below is
executed?
int a = 4;
int b = (int)Math.ceil(a % 3 + a / 3.0);
1
2
3
4
Correct Answer
C. Option 3
28.
Consider the following code and choose the
correct option:
class Test{
class A{
interface X{
int z=4; } }
static void display(){
System.out.println(new A().X.z); }
public static void main(String[] args) {
display(); }}
0
Compilation
error
Compiles but
error at run
time
4
Correct Answer
B. Option 2
29.
Consider the code below & select the correct
ouput from the options:
public class Test {
public static void main(String[] args) {
String[] elements = { "for", "tea", "too" };
String first = (elements.length > 0)
?elements[0] : null;
System.out.println(first); }}
Compilation
error
The variable
first is set to
null.
The variable
first is set to
elements[0].
Compiles but
error at
runtime
Correct Answer
C. Option 3
30.
Given the following piece of code:
public class Test {
public static void main(String args[]) {
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ) {
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
}
what will be the output?
0 6 1 7 2 8 3
8
0 6 1 7 2 8 3
9
0 5 1 5 2 5 3
5
compilation
fails
Correct Answer
A. Option 1
31.
Given
class MybitShift
{
public static void main(String [] args)
{
int a = 0x5000000;
System.out.print(a + " and ");
a = a >>> 25;
System.out.println(a);
}
}
83886080
and -2
2 and
83886080
2 and -
83886080
83886080
and 2
Correct Answer
D. Option 4
32.
Consider the code below & select the correct
ouput from the options:
public class Test {
int squares = 81;
public static void main(String[] args) {
new Test().go(); }
void go() {
incr(++squares);
System.out.println(squares); }
void incr(int squares) { squares += 10; } }
92
91
Compilation
error
82
Correct Answer
D. Option 4
33.
Class C{
public static void main (String[] args) {
byte b1=33; //1
b1++; //2
byte b2=55; //3
b2=b1+1; //4
System.out.println(b1+""+b2);
}}
Consider the code above & select the correct
output.
compile time
error at line 2
compile time
error at line 4
prints 34,56
runtime
exception
Correct Answer
B. Option 2
34.
What will be the output of the program ?
public class Test
{
public static void main(String [] args)
{
signed int x = 10;
for (int y=0; y<5; y++, x--)
System.out.print(x + ", ");
}
}
10, 9, 8, 7, 6,
9, 8, 7, 6, 5,
Compilation
fails
An exception
is thrown at
runtime
Correct Answer
C. Option 3
35.
1. public class LineUp {
2. public static void main(String[] args) {
3. double d = 12.345;
4. // insert code here
5. }
6. }
Which code fragment, inserted at line 4,
produces the output | 12.345|?
A. System.out.printf("|%7f| \n", d);
B. System.out.printf("|%3.7f| \n", d);
C. System.out.printf("|%7.3d| \n", d);
D. System.out.printf("|%7.3f| \n", d);
A
B
C
D
Correct Answer
D. Option 4
36.
Consider the following code and choose the
correct option:
class Test{
interface Y{
void display(); }
public static void main(String[] args) {
Y y=new Y(){
public void display(){
System.out.println("Hello World"); } };
y.display(); }}
Hello World
Compilation
error
Compiles but
error at run
time
Compiles but
run without
output
Correct Answer
A. Option 1
37.
Class Test{
public static void main(String[] args){
int var;
var = var +1;
System.out.println("var ="+var);
}}
consider the code above & select the proper
output from the options.
compiles and
runs with no
output
var = 1
does not compile
run time error
Correct Answer
C. Option 3
38.
State the class relationship that is being
implemented by the following code:
class Employee
{
private int empid;
private String ename;
public double getBonus()
{
Accounts acc = new Accounts();
return acc.calculateBonus();
}
}
class Accounts
{
public double calculateBonus(){//method's
code}
}
Aggregation
Simple Association
Dependency
Composition
Correct Answer
C. Option 3
39.
Given classes A, B, and C, where B extends
A, and C extends B, and where all classes
implement the instance method void doIt().
How can the doIt() method in A be
called from an instance method in C?
It is not possible
super.doIt()
his.super.doIt ()
((A)
this).doIt();
Correct Answer
A. Option 1
40.
Which of the following will declare an array
and initialize it with five numbers?
Array a =
new Array(5);
int [] a =
{23,22,21,20,
19};
int a [] = new
int[5];
int [5] array;
Correct Answer
B. Option 2
41.
Which of the following are correct variable
names? (Choose TWO)
int #ss;
int 1ah;
int _;
int $abc;
Correct Answer(s)
C. Option3
D. Option4
42.
What is the output of the following:
int a = 0;
int b = 10;
a = --b ;
System.out.println("a: " + a + " b: " + b );
a: 9 b:11
a: 10 b: 9
a: 9 b:9
a: 0 b:9
Correct Answer
C. Option 3
43.
As per the following code fragment, what is
the value of a?
String s;
int a;
s = "Foolish boy.";
a = s.indexOf("fool");
-1
0
4
random value
Correct Answer
A. Option 1
44.
Consider the following code snippet:
int i = 10;
int n = i++%5;
What are the values of i and n after the code
is executed?
10, 1
11, 1
10,0
11,0
Correct Answer
D. Option 4
45.
Consider the following code and choose the
correct output:
int value = 0;
int count = 1;
value = count++ ;
System.out.println("value: "+ value + " count:
" + count);
value: 0
count: 0
value: 0
count: 1
value: 1
count: 1
value: 1
count: 2
Correct Answer
D. Option 4
46.
Consider the following code and select the
correct output:
class Test{
interface Y{
void display(); }
public static void main(String[] args) {
new Y(){
public void display(){
System.out.println("Hello World"); } };
}}
Hello World
Compilation
error
Compiles but
error at run
time
Compiles but
run without
output
Correct Answer
D. Option 4
47.
What is the output of the following program?
public class demo {
public static void main(String[] args) {
int arr[5];
for (int i = 0; i < arr.length; i++) {
arr[i] = arr[i] + 10;
}
for (int j = 0; j < arr.length; j++)
System.out.println(arr[j]);
}
}
A sequence
of five 10's
are printed
A sequnce
of Garbage
Values are
printed
compile time
Error
Compiles but
no output
Correct Answer
C. Option 3
48.
Which of the following methods registers a
thread in a thread scheduler?
run();
construct();
start();
register();
Correct Answer
C. Option 3
49.
Class PingPong2 {
synchronized void hit(long n) {
for(int i = 1; i < 3; i++)
System.out.print(n + "-" + i + " ");
}
}
public class Tester implements Runnable {
static PingPong2 pp2 = new PingPong2();
public static void main(String[] args) {
new Thread(new Tester()).start();
new Thread(new Tester()).start();
}
public void run() {
pp2.hit(Thread.currentThread().getId()); }
}
Which statement is true?
The output
could be 5-1
6-1 6-2 5-2
The output
could be 6-1
6-2 5-1 5-2
The output
could be 6-1
5-2 6-2 5-1
The output
could be 6-1
6-2 5-1 7-1
Correct Answer
B. Option 2
50.
Consider the following code and choose the
correct option:
class Cthread extends Thread{
public void run(){
System.out.print("Hi");}
public static void main (String args[]){
Cthread th1=new Cthread();
th1.run();
th1.start();
th1.run();
}}
will print Hi
twice and
throws
Exception at
run time
will print Hi
Thrice
Compilation
error
will print Hi
once
Correct Answer
B. Option 2