Vous êtes sur la page 1sur 10

SCJ P 6 ExamSimulator (Trial): Trial Test

http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
Call Us at +44-208-133-7875 | Sign Out | My Cart | Change Password
Welcome Emmanuel ARON !
1
Marks: 1
a. Line 8
b. Line 9
c. Line 10
d. Line 11
e. None of these
Which of the following lines will print false?
1.public class MyClass
2.{
3. static String s1 = "I am unique!";
4. public static void main(String[] args)
5. {
6. String s2 = "I am unique!";
7. String s3 = new String(s1);
8. System.out.println(s1 == s2);
9. System.out.println(s1.equals(s2));
10. System.out.println(s3 == s1);
11. System.out.println(s3.equals(s1));
12. }
13.}
Choice C is the correct answer.
Line 10 will print false. If you construct multiple Strings using the same String literal without the new keyword, then
J ava creates only one String object. Hence, in the above code, the string references s1 and s2 refer to the same
anonymous string object, initialized with the character string: "I am unique!"
Thus s1 == s2 will return true and obviously, s1.equals(s2) will return true. But creating string objects using the
String(String s) constructor creates a new string, hence s3 == s1 will return false even though s3.equals(s1) will
return true. This is because s1 and s3 are referring to two different string objects whose contents are same.
More information is available at
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#12104
Incorrect
Marks for this submission: 0/1.
2
Marks: 1
Which of these will compile and run? Select two choices.
Home My Account SCJ P 6 Exam Simulator (Trial) Trial Test Review of attempt 1
Feedback to Author
Feedback to Author
Trial Test
Review of attempt 1
Started on Thursday, 6 J anuary 2011, 04:35 PM
Completed on Thursday, 6 J anuary 2011, 04:36 PM
Time taken 1 min 39 secs
Grade 0 out of a maximum of 14 (0%)
Feedback
FAIL: You need to study more!
Finish review
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
a. LinkedList<Integer>l=new LinkedList<int>();
b. List<Number>l=new LinkedList<Number>();
c. LinkedList<Integer>l=new LinkedList ();
d. LinkedList<Integer>l=new <Integer>LinkedList();
e. List<Number>l=new LinkedList<Integer>();
Choices B and C are the correct answers.
The List interface is implemented by the LinkedList class. So List<Number> is the super type of
LinkedList<Number>, which accepts only objects of type Number. LinkedList<Integer> declares a list that accepts
only Integer objects. Assigning a LinkedList raw type to it gives warnings of unsafe operations, however, it does
compile and run without errors.
Choice A is incorrect because generic parameters cannot be primitives.
Choice D is incorrect because the right hand side of the assignment uses the wrong syntax.
Choice E is incorrect because List<Number> is not a supertype of LinkedList<Integer>.
Reference:
http://java.sun.com/docs/books/tutorial/extra/generics/index.html
Incorrect
Marks for this submission: 0/1.
3
Marks: 1
a. for(Map.Entry pairs : map.entrySet()){}
b. Iterator i=map.entrySet().iterator();
c. Iterator i=map.iterator();
d. Iterator<Map.Entry<Integer,String>>i=map.entrySet().iterator();
e. Iterator<Map.Entry>i=map.entrySet().iterator();
Given the following code line.
Map<Integer,String> map = new HashMap<Integer,String>();
Which of the following are legal? Select three choices.
Choices A, B, and D are the correct answers.
A Map interface is implemented by an object that maps unique keys to values. Here, the key is of Integer type and
the value of String type. The HashMap class is an efficient implementation of the Map interface, which does not
guarantee any ordering for the keys. The entrySet() method of Map returns a Set view of the mappings contained in
this Map. Each element in the Set is a Map.Entry, from which the key and the value can be obtained.
Choice A is correct because enhanced for loop can be used to iterate through all collection types, including sets.
Choice B is correct because the iterator() method of a set returns the Iterator object which can be used to traverse
the set.
Choice C is incorrect because the Map interface does not define an iterator() method.
Choice D is correct because the objects iterated upon are of type Map.Entry<Integer,String>.
Choice E is incorrect because the object type is specified as Map.Entry instead of Map.Entry<Integer,String>.
For more information, check out:
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Incorrect
Marks for this submission: 0/1.
4
Marks: 1
What will be the result of attempting to compile and run the following program?
class MyThread extends Thread
Feedback to Author
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
a. Prints "BA"
b. Prints "AB"
c. Prints "Bmain"
d. Prints "mainB"
e. Output can be "Bmain" or "mainB"
f. Output can be "BA" or "AB"
g. Code does not compile
{
public void run()
{
m1();
}
MyThread(String threadName)
{
super(threadName);
}
public synchronized void m1()
{
System.out.print(Thread.currentThread().getName());
}
public static void main (String[] args)
{
MyThread a = new MyThread("A");
MyThread b = new MyThread("B");
b.start();
Thread.yield();
a.run();
}
}
Choice E is the correct answer.
Here, two new Thread objects are created, but only thread B is started in a separate thread of execution.
When the run() method is invoked directly on a Thread object, it does not start a new thread of execution. So in this
case, the run() method is run for the main thread and the thread B. So the output will contain "B" and "main", but
the order in which these appear cannot be predicted because it is dependent on the Thread Scheduler. So the output
can be "Bmain" or "mainB".
Incorrect
Marks for this submission: 0/1.
5
Marks: 1
a. By calling notify(5)
b. By calling notifyAll()
c. By calling myThread.notify()
d. By calling notify(myThread)
e. None of these
There are 10 threads waiting for the lock of an object. How will you bring the 5th thread myThread out of the
waiting state?
Choice B is the correct answer.
notify and notifyAll are instance (non static) methods of the Object class. The notify() method wakes up a single
thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be
awakened. It happens at the discretion of the thread scheduler and cannot be influenced.
Choices A and D are incorrect because the notify method does not take any arguments. Choice C is incorrect
because it is a method of the Object class and not of the Thread class.
Choice B is correct because the notifyAll() method moves all the waiting threads into the runnable state. So all the 10
threads (including the 5th one) are notified when the notifyAll() method is invoked.
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
Check out the API for more about the wait and notify methods:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html
Incorrect
Marks for this submission: 0/1.
6
Marks: 1
a. Code does not compile
b. Prints "Inner Constructor", "Outer" and "Sub Inner f"
c. Prints "Outer" and " Sub Inner f"
d. Prints "Outer", "Inner f" and "Sub Inner f"
e. Prints "Inner Constructor", "Outer", "Inner f" and "Sub Inner f"
f. None of the above
import java.util.*;
public class Test
{
void f()
{
System.out.println("Outer");
}
public class InnerTest
{
InnerTest()
{
System.out.println("Inner constructor");
}
void f()
{
System.out.println("Inner f");
}
}
public static void main(String[] args)
{
Test t=new Test();
Test.InnerTest test=t.new InnerTest()
{
public void f()
{
t.f();
System.out.println("Sub Inner f");
}
};
test.f();
}
}
Choice A is the correct answer.
The given code does not compile because we are attempting to access the method local variable t, from within the
local anonymous inner class. A method local class can access local variables in the enclosing method only if the
variables are final. So the compiler gives an error. Since the code does not even compile, no output is produced. So
all the other choices are incorrect.
Reference:
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
Incorrect
Marks for this submission: 0/1.
7
Marks: 1
What is the result of compiling and running the given code?
class A
{
int b=10;
Feedback to Author
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
a. Does not compile
b. Prints 0
c. Prints 10
d. Prints 7
private A()
{
this.b=7;
}
int f()
{
return b;
}
}
class B extends A{ int b; }
class Test
{
public static void main(String[] args)
{
A a=new B();
System.out.println(a.f());
}
}
Choice A is the correct answer.
The code does not compile because the constructor of class A is declared as private. This creates a problem when
the subclass constructor makes an implicit super() call to the parent class constructor at the time B is instantiated.
Since the code does not compile, all the other choices are incorrect. If the constructor of A had not been private, the
output would have been 7.
Incorrect
Marks for this submission: 0/1.
8
Marks: 1
Drag and drop the given excerpts in the boxes in the given code so that the output is
A
D
class ETest
{
public static void main(String[] args)
{
int i=1;
try
{
for ( ; true ; i-- )
{ //1
if(i / <=0)break;//2
}
}

{
System.out.println("A");
}

{
System.out.println("B");
}

{
System.out.println("C");
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
}

{
System.out.println("D");
}
}
}
catch(Exception e)
catch(RuntimeException e)
--i
catch(ArithmeticException e)
finally
i--
When an integer is divided by zero, it causes ArithmeticException to be thrown which gets caught by the first catch
block itself. "A" is printed.
RuntimeException is the superclass of ArithmeticException and Exception is the superclass of RuntimeException.
Catch blocks must be ordered from the specific one towards the general ones (for the code to compile without
errors), so the catch block for ArithmeticException must be followed by the catch block for RuntimeException, which
in turn would be followed by that for Exception. The finally block appears after all the catch blocks. So "D" would be
printed after "A".
Incorrect
Marks for this submission: 0/1.
9
Marks: 1
a. It will print-"Total =11200"
b. Compilation error at line 16
c. Compilation error at line 7
d. Compilation error at line 5
e. None of these
What will happen when you attempt to compile and run the following code? (Assume that the code is compiled and
run with assertions enabled.)
1. public class AssertTest
2. {
3. int LOCAL_RATE = 12;
4.
5. private void computeAmount(int rate)
6. {
7. assert rate = LOCAL_RATE : "Invalid rate";
8. int amount = 10000;
9. int total = amount + (amount * rate/100);
10. System.out.println("Total = " + total);
11. }
12.
13. public static void main(String[] args)
14. {
15. AssertTest test = new AssertTest();
16. test.computeAmount(12);
17. }
18. }
Choice C is the correct answer.
The syntax of assert requires first expression to be a boolean. This expression is of form -
assert Expression1 : Expression2
where Expression1 MUST be a boolean. In this code, the expression "rate = LOCAL_RATE" is not a boolean (note
the single =, instead of ==), hence it results in a compilation error at line 7.
There is nothing wrong with lines 16 and 5. Hence choices B and D are incorrect. The main method is contained in
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
the class AssertTest and hence it can access its private methods.
It is helpful to remember this form of an assert statement as follows -
assert boolean_expression : secondary_expression
Incorrect
Marks for this submission: 0/1.
10
Marks: 1
a. 2.7
b. 1.7
c. 0.0
d. -1.0
What value is printed out by executing the following code?
class MyClass
{
public static void main(String[] args)
{
double d = 2.7;
Box b = new Box();
b.doMinus(d);
System.out.println(d);
}
}
class Box
{
public double d;
public void doMinus(double d)
{
d = d - 1.0;
}
}
Choice A is the correct answer.
In J ava, all parameters are passed by value. In case of primitives, the copy of the variable is passed, while in case of
object references, it's the copy of the reference that is passed.
When the argument is a primitive type, pass-by-value means that the method can change the value of the passed
argument (in the method scope) but, the called method cannot change the value of the variable in the calling
method.
When the argument is of reference type, pass-by-value means that the method cannot change the object reference,
but can invoke the object's methods and modify the accessible variables within the object.
In the example above, d is passed as value, hence the change done in the doMinus() method doesn't reflect on d
outside the doMinus() method. Thus, the value printed is 2.7.
Incorrect
Marks for this submission: 0/1.
11
Marks: 1
What is the result of compiling and running the following code?
abstract class Test
{
void test()
{
System.out.println("Super");
}
}
protected class SubTest extends Test
{
void test()
Feedback to Author
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
a. Compiler Error: Abstract class Test must contain at least one abstract method
b. Prints "Super"
c. Prints "Sub"
d. None of these
{
System.out.println("Sub");
}
}
public class AccessTest
{
public static void main(String[] args)
{
Test t=new SubTest();
t.test();
}
}
Choice D is the correct answer.
The code does not compile because a non-nested class cannot be declared protected. For such a class, only public
and default access levels are allowed.
Choice A is incorrect because it is not mandatory for abstract classes to have abstract methods.
Since the code does not compile, choices B and C are automatically incorrect.
Incorrect
Marks for this submission: 0/1.
12
Marks: 1
a. It exhibits high cohesion
b. It exhibits low cohesion
c. It exhibits tight coupling
d. It exhibits loose coupling
Which of the following is true about the takeOrdersAndServe() method in the below code?
class Waitor
{
void takeOrders(){}
void serveFood(){}
void takeOrdersAndServe()
{
takeOrders();
serveFood();
}
}
Choice B is the correct answer.
Cohesion refers to the number and diversity of tasks that a single unit is responsible for. If each unit is responsible
for one single logical task, we say it has high cohesion.
Cohesion applies to classes and methods. Here the takeOrdersAndServe() method is not focused on a single task, so
it shows low cohesion. A well designed unit will have high cohesion.
More about coupling, cohesion and other related concepts here.
http://satc.gsfc.nasa.gov/support/STC_APR98/apply_oo/apply_oo.html
Incorrect
Marks for this submission: 0/1.
Feedback to Author
Feedback to Author
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
13
Marks: 1
Drag and drop the appropriate methods into the boxes in the code so that the given program compiles and runs
without error.
public class MyClass
{
public static void main(String[] argv) { }
void amethod(int i) { }

}
class MySubclass extends MyClass
{

}
protected void amethod(int i) throws Exception {}
private int amethod(float i){return 0;}
void amethod(int i) throws RuntimeException {}
public int amethod(int i) {return 0;}
For valid overloading, the argument list of the methods must be different, the return type may or may not differ. So
choice D is a valid way to overload the amethod method in the MyClass class. The access modifier does not matter
in overloading.
Choice C is a valid way to override the amethod method, since the return type and arguments match. Access
modifier is allowed to be the same or more public in overriding, so that is also not a problem. Though it is not
allowed to throw new checked exceptions, throwing unchecked exceptions is not a problem in overriding. Thus,
choice C is correct.
Choice A is not a valid way to override since it throws Exception which is not thrown by the original method in the
MyClass class.
Choice B is not a valid override or overload since the arguments match and only the return type is different.
More information about overloading is available at
http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#236017
Incorrect
Marks for this submission: 0/1.
14
Marks: 1
a. You can perform file I/O operations using its methods.
b. You can find the size of a file using its size() method.
c. You can check if a file exists using its exists() method.
d. You can delete a file using its delete() method.
Which of the following are true about the File class? Select two choices.
Choices C and D are the correct answers.
The exists() method of the File class returns true if the file given by the abstract path name physically exists.
The delete() method can be used to delete the file represented by the File object.
Choice A is incorrect because we cannot perform read/write operations on files using the File object.
Choice B is incorrect because the File class does not define a size() method, it defines the length() method to find
the size of a file.
Reference:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
Incorrect
Marks for this submission: 0/1.
Feedback to Author
Finish review
SCJ P 6 ExamSimulator (Trial): Trial Test
http://www.whizlabs.com/examprep/mod/quiz/review.php?attempt=124816&showall=true[07/01/2011 06:00:01]
SCJA Certification | SCJP Certification | SCWCD Certification | SCBCD Certification | SCDJWS Certification | SCMAD Certification | SCEA Certification
Legal | Policies | Copyrights and Trademarks

Vous aimerez peut-être aussi