Tuesday 30 July 2013

SCJP QUESTIONS

SUN CERTIFIED JAVA PROGRAMMER

Question - 1
What is the output for the below code ?
1. public class A {
2. int add(int i, int j){
3. return i+j;
4. }
5.}
6.public class B extends A{
7. public static void main(String argv[]){
8. short s = 9;
9. System.out.println(add(s,6));
10. }
11.}

Options are 
A.Compile fail due to error on line no 2
B.Compile fail due to error on line no 9
C.Compile fail due to error on line no 8
D.15

Question - 2
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();
 }
}
Options are 
A.0 false 0
B.0 true 0
C.0 0 0
D.Compile error - static variable must be initialized before use


Question - 3
What is the output for the below code ?
public class Test{
int _$;
int $7;
int do;
 public static void main(String argv[]){
 Test test = new Test();
 test.$7=7;
 test.do=9;
 System.out.println(test.$7);
 System.out.println(test.do);
 System.out.println(test._$);
 }
}
Options are
A.7 9 0
B.7 0 0
C.Compile error - $7 is not valid identifier.
D.Compile error - do is not valid identifier

Question -4:
What is the output for the below code ?
package com;
class Animal {
 public void printName(){
 System.out.println("Animal");
 }
}

package exam;
import com.Animal;
public class Cat extends Animal {
 public void printName(){
 System.out.println("Cat");
 }
}

package exam;
import com.Animal;
public class Test {
public static void main(String[] args){
 Animal a = new Cat();
 a.printName();
}
}
Options are 
A.Animal
B.Cat
C.Animal Cat
D.Compile Error

Question No-5:
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);
 }
}
Options are 
A.Value-B 11
B.Value-B 10
C.Value-A 10
D.Value-A 11

1 comment:

  1. Thanks for the questions. It must have been better if there were more questions on threads and generics, though. I did all exams on http://www.examlab.org and was looking for more.

    ReplyDelete