SCJP
SCJP
SCJP
}
}
Options are
A.t t1
B.t1 t
C.t t
D.Compilation succeed but Runtime Exception
Answer :
D is the correct answer.
Thread priorities are set using a positive integer, usually between 1 and 10.
t1.setPriority(-3); throws java.lang.IllegalArgumentException.
Questions no -2
What is the output for the below code ?
Options are
A.good
B.null
C.Compilation fails with an error at line 5
D.Compilation succeed but Runtime Exception
Answer :
A is the correct answer.
options
A)true
B)Compile Error - can't use Boolean object in if().
C)false
D)Compile Properly but Runtime Exception.
Correct answer is : A
options
A)59
B)Compile time error, because you have to do int total = ((Integer)
(list.get(0))).intValue();
C)Compile time error, because can't add primitive type in List.
D)Compile Properly but Runtime Exception.
Correct answer is : A
options
A)0
B)Compile with error
C)null
D)NullPointerException
Correct answer is : D
Explanations :An Integer expression can have a null value. If your program tries
to autounbox null,
it will throw a NullPointerException.
Questions no -4
What is the output for the below code ?
class Inner {
public void displayValue() {
System.out.println("Value of a is " + a);
}
}
}
options
A)Value of a is 7
B)Compile Error - not able to access private member.
C)Runtime Exception
D)Value of a is 8
Correct answer is : A
Explanations : An inner class instance can never stand alone without a direct
relationship to an instance of the outer class.
you can access the inner class is through a live instance of the outer class.
Questions no -5
What is the output for the below code ?
public class B {
options
A)Compile with error
B)USA
C)UK
D) Runtime Exception
Correct answer is : A
Explanations : You cannot have two methods in the same class with signatures
that only differ by return type.
Questions no -6
What is the output for the below code ?
public class C {
}
public class A {
public C getOBJ(){
System.out.println("class A - return C");
return new C();
public D getOBJ(){
System.out.println("class B - return D");
return new D();
}
}
options
A)Compile with error - Not allowed to override the return type of a method with
a subtype of the original type.
B)class A - return C
C)class B - return D
D) Runtime Exception
Correct answer is : C
Explanations : From J2SE 5.0 onwards. You are now allowed to override the
return type of a method with a subtype of the original type.
Questions no -7
What is the output for the below code ?
public class A {
options
A)Compile with error
B)Name-A
C)Name-C
D)Runtime Exception
Correct answer is : A
Overridden method should throw only same or sub class of the exception
thrown by super class method.
Questions no -8
What is the output for the below code ?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("b");
boolean b = m.matches();
System.out.println(b);
}
}
options
A)true
B)Compile Error
C)false
D)b
Correct answer is : A
Explanations : a*b means "a" may present zero or more time and "b" should be
present once.
Questions no -9
What is the output for the below code ?
}
}
options
A)1 2 red blue
B)Compile Error - because Scanner is not defind in java.
C)1 fish 2 fish red fish blue fish
D)1 fish 2 fish red blue fish
Correct answer is : A
Pattern p = Pattern.compile("a{3}b?c*");
Matcher m = p.matcher("aaab");
boolean b = m.matches();
System.out.println(b);
}
}
options
A)true
B)Compile Error
C)false
D)NullPointerException
Correct answer is : A
Explanations :
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times
Questions no -11
What is the output for the below code ?
Pattern p = Pattern.compile("a{1,3}b?c*");
Matcher m = p.matcher("aaab");
boolean b = m.matches();
System.out.println(b);
}
}
options
A)true
B)Compile Error
C)false
D)NullPointerException
Correct answer is : A
Explanations :
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times
Questions no -12
What is the output for the below code ?
public class A {
public A() {
System.out.println("A");
}
}
options
A)A B A
B)A B A B
C)B B
D)A B
Correct answer is : A
Explanations :On the time of deserialization , the Serializable object not create
new object. So constructor of class B does not called.
public class A {}
}catch(Exception e){
e.printStackTrace();
}
options
A)Compilation Fail
B)java.io.NotSerializableException: Because class A is not Serializable.
C)Run properly
D)Compilation Fail : Because class A is not Serializable.
Correct answer is : B
When JVM tries to serialize object B it will try to serialize A also because (A a =
new A()) is instance variable of Class B.
So thows NotSerializableException.
Questions no -14
What is the output for the below code running in the same JVM?
options
A)9 0 9
B)9 7 9
C)0 0 0
D)0 7 0
Correct answer is : A
In the case of static variable you can get the values in the same JVM.
Questions no -15
What is the output for the below code ?
options
A)7:30
B)Compile Error - an enum cannot be instantiated using the new operator.
C)12:50
D)19:45
Correct answer is : B
options
A)Frequency EnumMap: {MONDAY=12, TUESDAY=34, WEDNESDAY=56,
THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=78}
B)Compile Error
C)NullPointerException
D)Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56,
THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=123}
Correct answer is : C
enum SimpleMeal {
BREAKFAST, LUNCH, DINNER
}
}
}
options
A)Compile without error
B)Compile with error
C)Compile without error but Runtime Exception
D)Compile without error but Enum Exception
Correct answer is : B
Explanations :
An enum declaration is a special kind of class declaration:
Here in
public void foo() {
enum SimpleMeal {
BREAKFAST, LUNCH, DINNER
}
}
options
A)7:30
B)Compile Error
C)12:15
D)19:45
Correct answer is : B
In this case compile error because of declaration int t; before enum declaration.
Questions no -19
What is the output for the below code ?
options
A)A compile time error indicating that no run method is defined for the Thread
class
B)A run time error indicating that no run method is defined for the Thread class
C)Clean compile and at run time the values 0 to 9 are printed out
D)Clean compile but no output at runtime
Correct answer is : D
Explanations : This is a bit of a sneaky one as I have swapped around the names
of the methods you need to define and call when running a thread.
If the for loop were defined in a method called public void run() and the call in
the main method had been to b.start() The list of values from 0 to 9
would have been output.
Questions no -20
}
public void nameTest(String sName){
sName = sName + " idea ";
start();
}
public void run(){
}
}
options
A)good
B)good idea
C)good idea good idea
D)good 0 good 0 1
Correct answer is : A
}
}
}
public void run(){
for(int i =0; i < 2 ; i++){
try{
sleep(1000);
}catch(InterruptedException e){}
yield();
System.out.println(sTname);
}
}
}
options
A)Compile error
B)One One Two Two
C)One Two One Two
D)One Two
Correct answer is : B
Explanations : If you call the run method directly it just acts as any other method
and does not return to the calling code until it has finished. executing
Questions no -22
What is the output for the below code ?
options
A)Compilation clean and run but no output
B)Compilation and run with the output "Running"
C)Compile time error with complaint of no Thread import
D)Compile time error with complaint of no access to Thread package
Correct answer is : B
Explanations :
The Thread class is part of the core java.lang package and does not need any
explicit import statement.
Questions no -23
What is the output for the below code ?
options
A)anonymous tech
B)Compile Error
C)Tech
D)anonymous tech Tech
Correct answer is : A
Anonymous inner class ( anonymous subclass ) overriden the Tech super class of
tech() method.
options
A)Outer x is Outer variable.
B)Compile Error
C)Local variable z is local variable.
D)Outer x is Outer variable Local variable z is local variable
Correct answer is : B
}
}
options
A)The class compiles and runs, but does not print anything.
B)The number 2 gets printed with AssertionError
C)The number 3 gets printed with AssertionError
D)compile error
Correct answer is : B
}
}
options
A)The class compiles and runs, but does not print anything.
B)The number 2 gets printed with AssertionError
C)The number 3 gets printed with AssertionError
D)compile error
Correct answer is : A
Explanations : When if condition returns true, the assert statement also returns
true.
Hence AssertionError not generated.
Questions no -26
What is the output for the below code ?
NameBean(String str ){
this.str = str;
}
import java.util.HashSet;
myMap.add(s1);
myMap.add(s2);
myMap.add(s3);
myMap.add(s4);
System.out.println(myMap);
}
}
options
A)das abcdef abcdef
B)das das abcdef abcdef
C)das abcdef
D)abcdef abcdef
Correct answer is : A
options
A)Vector
B)ArrayList
C)Hashtable
D)HashMap
Correct answer is : A
Explanations : Vector implements List, RandomAccess -
Synchronized resizable-array implementation of the List
interface with additional "legacy methods."
Questions no -28
What is the output for the below code ?
public class Test {
options
A)Hello
B)Compile error
C)java.lang.ClassCastException
D)null
Correct answer is : C
options
A)Compile error : Integer can't add
B)newyork ca texas 11
C)newyork ca texas
D)newyork ca
Correct answer is : B
Explanations :
q was originally declared as Queue<String>, But in
show() method it is passed as an untyped Queue. nothing
in the compiler or JVM prevents us from adding an Integer
after that.
If the show method signature is public static void
show(Queue<String> q) than you can't add Integer, Only
String allowed. But public static void show(Queue q) is
untyped Queue so you can add Integer.
poll() Retrieves and removes the head of this queue,
or returns null if this queue is empty.
Questions no -30
What is the output for the below code ?
options
A)Compile with error
B)10
C)12
D) Runtime Exception
Correct answer is : A
Explanations : All the variables declared in interface is
Implicitly static and final , therefore can't change the
value.
Questions no -31
options
A)Compile with error
B)5
C)0
D) Runtime Exception
Correct answer is : A
class A {
{ System.out.print("b1 "); }
public A() { System.out.print("b2 "); }
}
class B extends A {
static { System.out.print("r1 "); }
public B() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class C extends B {
public static void main(String[] args) {
System.out.print("pre ");
new C();
System.out.println("post ");
}
}
options
A)r1 r4 pre b1 b2 r3 r2 post
B)r1 r4 pre b1 b2 post
C)r1 r4 pre b1 b2 post r3 r2
D)pre r1 r4 b1 b2 r2 r3 post
Correct answer is : A
Questions no -33
What is the output for the below code ?
options
A)true
B)false
C)Compile Error
D) Runtime Exception
Correct answer is : A
options
A)outer class can only declare public , abstract and final
B)outer class may be private
C)outer class can't be abstract
D)outer class can be static
Correct answer is : A
switch(c){
case 65:
System.out.println("one");break;
case 'a':
System.out.println("two");break;
case 3:
System.out.println("three");
}
options
A)one
B)two
C)Compile error - char can't be permitted in switch
statement
D)Compile error - Illegal modifier for the class Test; only
public, abstract & final are permitted.
Correct answer is : D
for(int i:list)
System.out.println(i);
}
}
options
A)1 2 3
B)Compile error , can't add primitive type in ArrayList
C)Compile error on for(int i:list) , Incorrect Syntax
D)0 0 0
Correct answer is : A
}
Questions no -37
What is the output for the below code ?
options
A)Overridden: hello (String, Integer[])
B)hello (String, Integer[])
C)This code throws an Exception at Runtime
D)Compile with error
Correct answer is : D
Explanations : Exception Exception is not compatible with
throws clause in SuperClass.doIt(String, Integer[]).
The same exception or subclass of that exception is allowed.
Questions no -38
What is the result of executing the following code, using
the parameters 0 and 3 ?
options
A)Prints out: Exception Finally
B)Prints out: Finally
C)Prints out: Exception
D)Compile with error
Correct answer is : B
options
A)An Error is a subclass of Throwable
B)An Error is a subclass of Exception
C)Error indicates serious problems that a reasonable
application should not try to catch.
D)An Error is a subclass of IOException
options
A)IOException
B)ArrayIndexOutOfBoundsException
C)Exception
D)Error
Correct answer is : B
options
A)true true
B)false false
C)false true
D)true false
Correct answer is : C
options
A)closing the stream
B)flushing the stream
C)writting to the stream
D)writting a line separator to the stream
Correct answer is : D
options
A)one two three
B)one
C)one two
D)Compile error.
Correct answer is : A
switch(c){
case 65:
System.out.println("one");break;
case 'a':
System.out.println("two");break;
case 3:
System.out.println("three");
}
options
A)one two three
B)one
C)two
D)Compile error - char can't be in switch statement.
Correct answer is : C
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
navMap.pollFirstEntry();
navMap.pollLastEntry();
navMap.pollFirstEntry();
System.out.println(navMap.size());
}
}
options
A)Compile error : No method name like pollFirstEntry() or
pollLastEntry()
B)3
C)6
D)4
Correct answer is : B
Explanations :
pollFirstEntry() Removes and returns a key-value mapping
associated with the least key in this map, or null if the
map is empty.
pollLastEntry() Removes and returns a key-value mapping
associated with the greatest key in this map, or null if the
map is empty.
Questions no -46
What is the output for the below code ?
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
System.out.print(navMap.lastEntry());
}
}
options
A)Compile error : No method name like lastEntry()
B)null
C)NullPointerException
D)0
Correct answer is : B
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
navMap.put(4, "April");
navMap.put(5, "May");
navMap.put(6, "June");
navMap.put(1, "January");
navMap.put(2, "February");
System.out.print(navMap.ceilingKey(3));
}
}
options
A)Compile error : No method name like ceilingKey()
B)null
C)NullPointerException
D)4
Correct answer is : D
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Test {
public static void main(String... args) {
navMap.put(4, "April");
navMap.put(5, "May");
navMap.put(6, "June");
navMap.put(1, "January");
navMap.put(2, "February");
System.out.print(navMap.floorKey(3));
}
}
options
A)Compile error : No method name like floorKey()
B)null
C)NullPointerException
D)2
Correct answer is : D
}
}
options
A)Compile error : No method name like tailSet()
B)6 34
C)5
D)5 6 34
Correct answer is : B
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class Test {
public static void main(String... args) {
}
}
options
A)Compile error : No method name like pollFirst() or
pollLast()
B)0
C)3
D)5
Correct answer is : B
Explanations :
pollFirst() Retrieves and removes the first (lowest)
element, or returns null if this set is empty.
pollLast() Retrieves and removes the last (highest)
element, or returns null if this set is empty.
Questions no -51
What is the output for the below code ?
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
}
}
options
A)1 2 7 10 34 null
B)2 7 null
C)2 7 34
D)1 2 7 10 34
Correct answer is : B
Explanations :
lower() Returns the greatest element in this set strictly
less than the given element, or null if there is no such
element.
higher() Returns the least element in this set strictly
greater than the given element, or null if there is no such
element.
Questions no -52
What is the output for the below code ?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
}
}
options
A)Compile error : No method name like headSet()
B)2, 6, 7, 8, 10
C)2, 6, 7, 8
D)34
Correct answer is : C
Explanations :
headSet(10) Returns the elements elements are strictly
less than 10.
headSet(10,false) Returns the elements elements are
strictly less than 10.
headSet(10,true) Returns the elements elements are
strictly less than or equal to 10.
Questions no -53
What is the output for the below code ?
import java.io.Console;
if (con != null)
{
int count = 0;
do
{
String uname = con.readLine(null);
char[] pwd = con.readPassword("Enter %s's
password: ", uname);
con.writer().write("\n\n");
} while (!auth && ++count < 3);
}
}
}
options
A)NullPointerException
B)It works properly
C)Compile Error : No readPassword() method in Console class.
D)null
Correct answer is : A