Sun Certified Java Programmer (SCJP) MCQs

Sun Certified Java Programmer (SCJP) MCQs

Try to answer these 20 Sun Certified Java Programmer (SCJP) MCQs and check your understanding of the Sun Certified Java Programmer (SCJP) subject. Scroll down and let's begin!

1:

What is the output for the below code?

public class C {

}

public class D extends C{

}

public class A {

public C getOBJ(){

System.out.println(“class A – return C”);

return new C();

}

}

public class B extends A{

public D getOBJ(){

System.out.println(“class B – return D”);

return new D();

}

}

public class Test {

public static void main(String… args) {

A a = new B();

a.getOBJ();

}

}


A.  

class B – return D


B.  

Compilation succeed but no output

C.  

Compilation fails


D.  

class A – return C


2:

What is the output for the below code?

1. public class Test {

2. public static void main(String[] args){

3. byte b = 6;

4. b+=8;

5. System.out.println(b);

6. b = b+7;

7. System.out.println(b);

8. }

9. }


A.  

14 13


B.  

14 21


C.  

Compilation fails with an error at line 6


D.  

Compilation fails with an error at line 4


3:

try{

File f = new File(“a.txt”);

}catch(Exception e){

}catch(IOException io){

}

Is this code create new file name a.txt?


A.  

False

B.  

True

C.  

Compilation Error


D.  

None

4:

What is the output for the below code?

public class A {

int i = 10;

public void printValue() {

System.out.println(“Value-A”);

}

}


public class B extends A{

int i = 12;

public void printValue() {

System.out.print(“Value-B”);

}

}


public class Test{

public static void main(String argv[]){

A a = new B();

a.printValue();

System.out.println(a.i);

}

}


A.  

Value-A 11


B.  

Value-B 11


C.  

Value-B 10


D.  

Value-A 10


5:

Find the given file.

package com.concretepage;

public class Test {

public enum Days { MON, TUE, WED, THU };

public static void main(String[] args) {

for(Days d : Days.values() );

Days [] d2 = Days.values();

System.out.println(d2[3]);

}

}

What is the result?


A.  

The output is unpredictable


B.  

WED

C.  

THU

D.  

Compilation fails due to an error on line 4


6:

Choose the correct statement.

A.  

"A extends B" is correct if and only if A is a class and B is an interface

B.  

"A extends B" is correct if and only if A is an interface and B is a class

C.  

"A extends B" is correct if A and B are either both classes or both interfaces

D.  

"A extends B" is correct for all combinations of A and B being classes or interfaces

7:

What is the output for the below code?

public class A {

public void printName(){

System.out.println(“Value-A”);

}

}

public class B extends A{

public void printName(){

System.out.println(“Name-B”);

}

}

public class C extends A{

public void printName(){

System.out.println(“Name-C”);

}

}

1. public class Test{

2. public static void main (String[] args) {

3. B b = new B();

4. C c = new C();

5. b = c;

6. newPrint(b);

7. }

8. public static void newPrint(A a){

9. a.printName();

10. }

11. }


A.  

Compilation fails due to an error on lines 5


B.  

Compilation fails due to an error on lines 9


C.  

Name-B


D.  

Name-C


8:

Find the given file.

package com.concretepage;

enum Day {

MON("1"), TUE("2"), WED("3");

String s;

Day(String s) { this.s = s; }

}

public class Test {

static Day d;

public static void main(String[] args) {

System.out.println(d.MON.s + " " + d.TUE.s);

}

}

What is the result?


A.  

1 2


B.  

Compilation fails due to an error on line 5


C.  

Compilation fails due to an error on line 10


D.  

Multiple compilation errors


9:

What is the output for the below code?

interface A {

public void printValue();

}

1. public class Test{

2. public static void main (String[] args){

3. A a1 = new A() {

4. public void printValue(){

5. System.out.println(“A”);

6. }

7. };

8. a1.printValue();

9. }

10. }


A.  

Compilation fails due to an error on line 8


B.  

Compilation fails due to an error on line 3


C.  

A

D.  

NULL

10:

 What is the output for the below code?

public class A {

static{System.out.println(“static”);}

{ System.out.println(“block”);}

public A(){

System.out.println(“A”);

}

public static void main(String[] args){

A a = new A();

}

}


A.  

static block A


B.  

A

C.  

A block static


D.  

static A


11:

Find the given file.

package com.concretepage;

class Road {

public static void main(String[] args) {

for(int __x = 0; __x < 3; __x++) ;

int #lb = 7;

long [] x [5];

Boolean []ba[];

}

enum Traffic { RED, YELLOW, GREEN };

}

What is the result?


A.  

Compilation fails with an error on line 5


B.  

Compilation fails with an error on line 7


C.  

Compilation succeeds


D.  

Compilation fails with an error on line 8


12:

ou have two class files name Test.class and Test1.class inside javaproject directory.

Test.java source code is:

public class Test{

public static void main (String[] args){

System.out.println(“Hello Test”);

}

}

Test1.java source code is :

public class Test1{

public static void main (String[] args){

System.out.println(“Hello Test1″);

}

}

you have issued below commands from command prompt.

cd javaproject

java Test Test1

What is the output?


A.  

Hello Test


B.  

Hello Test Hello Test1


C.  

Run fails – class not found


D.  

Hello Test1


13:

What is the output for the below code?

public class Test {

public static void main(String[] args){

String value = “abc”;

changeValue(value);

System.out.println(value);

}

public static void changeValue(String a){

a = “xyz”;

}

}


A.  

abc

B.  

Compilation clean but no output


C.  

xyz

D.  

Compilation fails


14:

What will be the result of compiling the following code:

public class SuperClass {

public int doIt(String str, Integer… data)throws Exception{

String signature = “(String, Integer[])”;

System.out.println(str + ” ” + signature);

return 1;

}

}

public class SubClass extends SuperClass{

public int doIt(String str, Integer… data)

{

String signature = “(String, Integer[])”;

System.out.println(“Overridden: ” + str + ” ” +

signature);

return 0;

}

public static void main(String… args)

{

SuperClass sb = new SubClass();

sb.doIt(“hello”, 3);

}

}


A.  

-hello (String, Integer[])


B.  

None of these


C.  

Complilation fails


D.  

hello (String, Integer[])


15: Method which follows JavaBeans standard?

A.   PutData

B.   GetCust

C.   AddNum

D.   DeleteEmp

16:

Find the given file.

package com.concretepage;

public class Computer implements Device{

public void doIt() { }

}

abstract class Phone1 extends Computer { }

abstract class Phone2 extends Computer{

public void doIt(int x) { }

}

class Phone3 extends Computer implements Device{

public void doStuff() { }

}

interface Device {

public void doIt();

}


What is the result?


A.  

Compilation succeeds


B.  

Compilation fails with an error on line 10


C.  

Compilation fails with an error on line 3


D.  

Compilation fails with an error on line 6


17:

What is the output for the below code ?


public class A {

int k;

boolean istrue;

static int p;

public void printValue() {

System.out.print(k);

System.out.print(istrue);

System.out.print(p);

}

}

public class Test{

public static void main(String argv[]){

A a = new A();

a.printValue();

}

}


A.  

0 true 0


B.  

0 0 0


C.  

0 false 0


D.  

Compile error – static variable must be initialized before use.


18:

class A {

A(String s) {

}

A() {

}

}

1. class B extends A {

2. B() { }

3. B(String s) {

4. super(s);

5. }

6. void test() {

7. // insert code here

8. }

9. }

Which of the below code can be insert at line 7 to make clean

compilation?


A.  

A a = new B(5);


B.  

A a = new B();


C.  

A a = new A(String s);


D.  

All of these


19:

What is the output for the below code?

public class A {

int add(int i, int j){

return i+j;

}

}

public class B extends A{

public static void main(String argv[]){

short s = 9;

System.out.println(add(s,6));

}

}


A.  

Compile fail due to error on line no 2


B.  

Compile fail due to error on line no 8


C.  

Compile fail due to error on line no 9


D.  

15

20:

Find the given file.


package com.concretepage;

public class A extends B {

public static void main(String[] args) {

Short myNum = 7;

System.out.println(add(myNum, 6));

}

}

class B {

int add(int x, int y) { return x + y; }

}


What is the result?


A.  

Compilation fails due to an error on line 5

B.  

Compilation fails due to an error on line 9


C.  

Compilation fails due to multiple errors


D.  

13