You can teach a student a lesson for a day; but if you can teach him to learn by creating curiosity, he will continue the learning process as long as he lives.

Tuesday, June 12, 2012

JAVA SCJP Questions and Answers

JAVA SCJP Questions and Answers

Objective type questions for preparation of SCJP exam and answers in java

(1)

publicclass Loop {

publicstaticvoid main(String[] args){

for(int i=0;false;i++){

System.out.println("java");

}

}

}

What will be output of above program?

(a)java

(b) Null

(c)It will not print anything.

(d)Compiler error

Answer: (d)

(2)What will be output of following program?

publicclass Datatype {

publicstaticvoid main(String[] args) {

byte b=127;

b++;

b++;

System.out.println(b);

}

}

(a)129

(b)2

(c)-127

(d)Compiler error

Answer: (c)

Explanation:

Range of byte data in java is -128 to 127. But byte data type in java is cyclic in nature.

(3)What will be output of following program?

class StringLiteral{

publicstaticvoid main(String[] args){

String str="local\national";

System.out.print(str);

}

}

(a)local\national

(b)local ational

(c)local

ational

(d)Compiler error

Answer: (c)

(4)

publicclass TypeConversion {

publicstaticvoid main(String[] args) {

double d=2D+2d+2.+2l+2L+2f+2F+2.f+2.D;

System.out.println(d);

}

}

(a)18.0

(b)9.0

(c)Run time exception

(d) Compiler error

Answer: (a)

(5)

publicclass UnaryOperator {

publicstaticvoid main(String[] args) {

byte a=-5;

int b=-5;

int c=~a+(byte)~b;

System.out.print(c);

}

}

What will output when you compile and run the above code?

(a)0

(b)8

(c)Run time exception

(d)Compiler error

Answer: (b)

(6)

publicclass BreakDemo {

publicstaticvoid main(String[] args){

int j=~-3;

while(j<7){

System.out.print(j);

if(j==3){

j+=2;

continue;

}

j++;

}

}

}

What will be output of above program?

(a)2367

(b)23

(c)2356

(d)Compiler error

Answer: (c)

(7)What will be output of following program?

publicclass Datatype {

publicstaticvoid main(String[] args) {

byte b=125;

System.out.println(b*2);

}

}

(a)250

(b)-6

(c)Runtime exception

(d)Compiler error

Explanation:

Answer: (a)

(8)

publicclass Identifier {

publicstaticvoid main(String[] args) {

doublestrictfp=5.02;

strictfp+=.333;

System.out.print(strictfp);

}

}

(a) 5.353

(b) 5.353D

(c) 5.353d

(d) Compiler error

Answer: (d)

In java programming language variable name includes alphabet, digits
few special characters like underscore (_), dollar singe ($) but it
should not be any reserved word of java language. Here strictfp is keyword of java cannot be name of variable

(9)

publicclass TypeConversion {

publicstaticvoid main(String[] args) {

float f1=11;

float f2=11.f;

f2=f1+f2;

System.out.println(f2);

}

}

What will output when you compile and run the above code?

(a)22

(b)22.0

(c)22.0f

(d) Compiler error

Answer: (b)

(10)

publicclass Operator {

publicstaticvoid main(String[] args) {

byte a=5;

int b=10;

int c=a>>2+b>>2;

System.out.print(c);

}

}

What will output when you compile and run the above code?

(a)60

(b)3

(c)0

(d)Compiler error

Answer: (c)

(11)

class Mango{

finalint a=5;

}

class Fruit extends Mango {

finalint a=10;

}

class DynamicDispatch extends Fruit{

finalint a=20;

publicstaticvoid main(String[] args){

Mango m=new DynamicDispatch();

Fruit f=new DynamicDispatch();

System.out.print(m.a|f.a);

}

}

What will be output of above program?

(a)15

(b)20

(c)30

(d)Compiler error

Answer: (a)

(12)

class Mango{

finalint a=5;

}

class Fruit extends Mango {

finalint a=10;

}

class DynamicDispatch extends Fruit{

finalint a=15;

publicstaticvoid main(String[] args){

DynamicDispatch t=new Mango();

System.out.print(t.a);

}

}

What will be output of above program?

(a)5

(b)10

(c)15

(d)Compiler error

Answer: (d)

(13)

Which is not true java statement?

(a)Java deallocates memory automatically.

(b) Finalize method is just called before garbage collection.

(c)Garbage collection runs when there is reference with object and runs periodically.

(d)Inside finalize method we keep those code which must be executed before object is destroyed by garbage collection.