You are on page 1of 3

Newsletter:

(Quiz) Some SCJP Questions with their Answers


Submitted by guru on Fri, 12/19/2008 - 11:55
QUIZ SCJP
Quiz : Some SCJP Questions with their Answers
Q 1 What is the output of the following code when compiled and run? Select two c
orrect answers.
public class TechnoSample {
public static void main(String[] args){
for(int i = 0; i <>
System.out.println(getPrimitive(127)); //line 1
}
}
public static int getPrimitive(byte b) { //line 2
return (short)(Math.random()*b); //line 3
}
}

(1) Compilation error on line 1


(2) Compilation error on line 2
(3) Compilation error on line 3
(4) Line 3 compiles fine
(5) Prints 10 random numbers between 0 and 127
Answer : 1,4
Explanation :
Line 1 does not compile because getPrimitive() takes a byte and we pass it an in
t. In a normal assignment (byte b = 127;) this would work because 127 is in the
range for byte values and the compiler implicitely does a norrowing conversion.B
ut this is not the case in method invocations. A quote from JLS 5.3: "The design
ers of the Java programming language felt that including these implicit narrowin
g conversions would add additional complexity to the overloaded method matchingr
esolution process". This speaks for itself. Line 3 compiles fine because we have
to do with a widening primitive conversion form short to int which is perfectly
straightforward.

Q2 Select three correct statements.


(1) A static method may override another static method
(2) A static method cannot override a non-static method
(3) A non-static method cannot override a static method
(4) A non-static method may be overloaded by a static method
(5) A synchronized method cannot be overridden
Answer : 2,3,4
Explanation :
Overriding is for non-static methods and hiding is for static methods. So the fo
llowing statements are the only true statements about hiding and overriding:
a static method (in a subclass) may hide another static method (in a superclass)
a static method (in a subclass) cannot hide a non-static method (in a superclass
)
a non-static method (in a subclass) may override another non-static method (in a
superclass)
a non-static method (in a subclass) cannot override a static method (in a superc
lass)
Q3 Select three correct statements about the following code.
public class TechnoSample {
public static void main(String[] args) {
TechnoSample myref = new TechnoSampleSub();
try{
myref.test();
}
catch(Exception e){}
}
void test() throws Exception{
System.out.println("In TechnoSample");
throw new Exception();
}
}
class TechnoSample Sub extends TechnoSample {
void test() {
System.out.println("In TechnoSampleSub");
}
}
(1) The try-catch block that encloses myref.test(); is mandatory for the code to
compile
(2) Prints: In TechnoSample
(3) Prints: In TechnoSampleSub
(4) Method test() in class TechnoSampleSub has no obligation to declare a throws
clause
(5) An exception is thrown at runtime
Answer : 1,3,4
Explanation :
myref is an instance of class TechnoSampleSub referenced by a variable of type T
echnoSample. Method test() in class TechnoSample is overridden in class TechnoSa
mpleSub, thus the one to be invoked is the one declared in class TechnoSampleSub
(Polymorphism!). Moreover, test() has no obligation to declare a throws clause (
see overriding rules!). The try-catch block is mandatory because myref could as
well reference an instanceof class TechnoSample and in that case the method test
() to be invoked would be the one declared in class TechnoSample which throws an
exception.
Q4 Given the following code:
import java.util.Date;
public class Example {
public static void main(String args[]) {
Date d1 = new Date (99, 11, 31);
Date d2 = new Date (99, 11, 31);
method(d1, d2);
System.out.println("d1 is " + d1 + "\nd2 is " + d2);
}
public static void method(Date d1, Date d2) {
d2.setYear (100);
d1 = d2;
}
}
Which one or more of the following correctly describe the behavior when this pro
gram is compiled and run?

(1) compilation is successful and the output is:


d1 is Fri December 31 00:00:00 GMT 1999 d2 is Fri December 31 00:00:00 GMT 1999
(2) compilation is successful and the output is:
d1 is Fri December 31 00:00:00 GMT 1999 d2 is Sun December 31 00:00:00 GMT 2000
(3) compilation is successful and the output is:
d1 is Sun December 31 00:00:00 GMT 2000 d2 is Sun December 31 00:00:00 GMT 2000
(4) the assignment 'd1 = d2' is rejected by the compiler because the Date class
cannot overload the operator '='
(5) the expression (d1 is " + d1 + "\nd2 is " + d2) is rejected by the compiler
because the Date class cannot overload the operator '+'
Answer : 2
Explanation :
1) is false because we know that the data in d2 was changed. 3) is false because
we know that the data in d1 was not changed. The names d1 and d2 are used in bo
th main and method to be confusing. They are different and stored on the stack i
n different place. All the interesting stuff that happen in the Example class is
in method. main simply initializes some data and prints the results. In method,
the following happens:
1.d2 has its year set to 100 (really 2000, as 2.Object d1 is set to be the same
as d2. This is a change of the actual reference, not in the data at d1.
Both of these line are perfectly legal, and do not result in a compilation error
, so d) is false. I will also point out here that e) is String context. toString
() is defined by the Object class and so it is available on all classes in Java.
Most non-trivial classes override toString() to return more explicit informatio
n about themselves.

You might also like