Java Mock Tests For SCJP
Java Mock Tests For SCJP
4)
JavaBeat Home SCJP 1.4 Home Objectives Forums Mock Exams Online Mock Exam Resources
Mock Exams
MockQuestions - 1 MockQuestions - 2 MockQuestions - 3 MockQuestions - 4
MockQuestions - 5 MockQuestions - 6 MockQuestions - 7 MockQuestions - 8
MockQuestions - 9 MockQuestions - 10 MockQuestions - 11 MockQuestions - 12
MockQuestions - 13 MockQuestions - 14 MockQuestions - 15 MockQuestions - 16
MockQuestions - 17 MockQuestions - 18 MockQuestions - 19 MockQuestions - 20
Question 1.
if( "String".endsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
1. True
2. False
Question 2.
if( "String".startsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
1. True
2. False
Question 3.
Question 4.
nt1.run();
nt2.run();
nt3.run();
}
}
Question 5.
Question 6.
Read the following code, which is a part of a synchronized method of a monitor.
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
//do some crap here.
}
//more and more code here
}
1. The code causes compilation error - sleep cannot be called inside synchronized methods.
2. The code causes compilation error - sleep is not a static method of java.lang.Thread
3. The Thread sleeps for at least 500 milliseconds in this method if not interrupted.
4. When the thread "goes to sleep" it releases the lock on the object.
5. The "sleeping" Threads always have the lock on the Object.
Question 7.
The no-argument constructor provided by the compiler when no constructor is explicitly provided in the code
1. is always public
2. is always "friendly"
3. always defaults to the access modifier provided for the class.
4. depends on the compilation options of javac
Question 8.
1. java.lang.Object.
2. java.util.EventObect
Question 9.
1. public
2. none (i.e., no access modifier).
3. private.
4. static
5. native
6. synchronized.
Question 10.
Which of the following are true about the class defined inside an interface
Question 11.
(0.0 == -0.0)
1. true
2. false
Question 12.
System.out.println(Math.min(Float.NaN, Float.POSITIVE_INFINITY));
1. NaN
2. Infinity
3. The expression throws a runtime exception - NaN is an illegal argument.
Question 13.
Math.max(Float.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
1. Float.POSITIVE_INFINITY
2. Double.POSITIVE_INFINITY
3. runtime Exception
Question 614.
System.out.println(s2);
}
}
Question 15.
Assuming that all the code inside the method is legal and cannot be a cause for compilation error -
1. An attempt to compile will cause a compilation error. Static methods cannot be synchronized.
2. Compilation will be successfull. The Object instantiated will have a lock which has to be gained by Threads
calling any of the two methods.
3. Compilation will be successfull. There will exist a Class wide lock which will have to be gained by the Thread
calling method1 and an instance lock for each instance which will have to be gained by the Thread making a call
to method2 on the Object instance. The class wide lock and the instance lock being independent of each other.
4. Compilation will be successfull. There will exist a Class wide lock which will have to be gained by the Thread
calling method1 and an instance lock for each instance which will have to be gained by the Thread making a call
to method2 on the Object instance. The class wide lock and the instance lock being mutually exclusive.
Question 16.
Question 67.
Assume that Cat is a class and String[] args is the argument passed to the public static void main(String
args[]) method of the class. The class is executed with the following command line string
System.out.println(args.length);
1. The above expression will cause a '0' appear on the command line.
2. Will throw a NullPointerException.
3. Will a blank line to appear.
Question 18.
Question 19.
Assume that th is an instance holding a thread object. th.start() causes the thread to start running and eventually
complete its execution. The object reference by th is not accessable any more and is garbage collected when the
garbage collecter runs.
1. True
2. False
Question 20.
import java.io.*;
System.out = pr;
1. The code causes a compiler error. out is a declared final in System and cannot be assigned to pr.
2. The code causes a runtime Exception due the assignment to a final variable.
3. The code compiles and runs success fully. A file called "outfile" is created and "Lets see what I see now??" is
printed in the same.
Question 21.
Question 22.
import java.io.IOException;
It is required that none of the constructors of Question72E should throw any checked exception.
1. It can be achived by placing the call to the superclass with a super keyword , which is placed in a try block
with a catch block to handle the IOException thrown by the super class.
2. It can be achived by avoiding explicit calls to the base class constructor.
3. It cannot be done in the Java Laungage with the above definition of the base class.
Question 23.
Read the following piece of code carefully. (This is an extention to question 72.)
1. will cause a compiler error - non-abstract classes cannot be extended to abstract classes.
2. will cause a compiler error - a constructor must be provided which may or may not throw an IOException
3. will cause a compiler error - a constructor must be provided which must throw an IOException or one of its
super types.
4. will not cause any compiler error. The class definition is perfectly legal.
Question 24.
class Base
{
Base()
{
System.out.println("Message 1 : In the base class constructor");
}
}
1. will cause a compiler error. The non-abstract classes cannot be extended to the abstract classes.
2. will cause a compiler error. The abstract classes cannot have constructors defined.
3. will not cause any compiler error. The lines "Message 1..." and "Message 3 ... " are printed on the screen.
4. will not cause any compiler error. The lines "Message 1..." and "Message 2...." and Message 3...." are printed
on the screen.
Question 25.
public class A
{
A()
{
}
}
Question 27.
If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection
Question 28.
Question 29.
1. is public
2. implements the java.util.Collection interface.
Question 30.
import java.io.*;
class K
{
public static void main(String args[]) throws IOException, ClassNotFoundException
{
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.out"));
out.writeObject(tw);
Question 31.
1. Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
2. Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with
"throws Exception".
3. Will cause no compile time error and print "I will never throw and Exception the screen".
4. Will Cause a run time error .
Question 32.
1. Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main
(String))
2. Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be
declared with "throws Exception".
3. Will cause no compile time error and print "I will never throw and Exception the screen".
4. Will Cause a run time error .
Question 33
//Contents of Constants.java
//Contents of SomeClass.java
The Constants.java file is edited and the greetingString is changed to "Hello World!". The file
after editing is shown below.
}
On Compilation of Constants.java and running of the application gives. (Note : The client class
SomeClass is not compiled)
Question 34.
import java.util.*;
Question 35.
import java.util.*;
An attempt to compile and call the someMethod of the above class at runtime will cause
1. A compilation error : v is not initialized in all constructors
2. A compilation error : v is not an instance variable
3. "true" is printed on the console
4. "false" is printed on the console
Question 36.
1. A compilation error : The final variable is not initialized in all the constructors.
2. A compilation error : The final instance variable is reassigned after initialization in the constructor
3. No compilation error : But will cause a run time exception (NullPointerException) if someMethod is called on an
instance that was created through the constructor with the integer argument.
4. A compilation error : The final instance variable is not initialized in the declaration
Question 37.
public class A
{
final StringBuffer sb = new StringBuffer("I am final");
public A()
{
}
public StringBuffer getSb()
{
return this.sb;
}
Question 38.
public class A
{
StringBuffer sb = new StringBuffer("I am final");
public A()
{
}
public final StringBuffer getSb()
{
return this.sb;
}
public class A
{
StringBuffer sb = new StringBuffer("I am final");
public A()
{
}
public StringBuffer getSb()
{
return this.sb;
}
Question 40.
A(int i)
{
System.out.println("No B this time");
}
}
class C
{
public static void main(String[] args)
{
A a = new A(1);
}
}
1. Compilation error : Inner class inside method cannot have static members or blocks
2. Compilation error : Inner classes cannot be delared inside constructors
3. No compilation error : At runtime Class B is not loaded
4. No compilation error : At runtime class B is loaded and the message "I am getting loaded" is printed on the
console.
Question 41.
Read the following code excerpt carefully. //Contents of file A.java public class A
{
A()
{
class B
{
{
System.out.println("I am in no-arg constructor");
}
}
}
A(int i)
{
class B
{
{
System.out.println("I am in the arg constructor");
}
}
new B();
}
}
class C
{
public static void main(String[] args)
{
A a = new A(1);
}
}
Which of the following are true
Answers:
1. 1
2. 1
3. No
4. 3
5. 2,4
6. 3,5
7. 3
8. 2
9. 1,2
10. 2,3,4
11. 1
12. 1
13. 2
14. 3
15. 3
16. 2,3
17. 1
18. 2,4
19. 2
20. 1
21. 5
22. 3
23. 3
24. 4
25. 1,2,3
26. 1
27. 3
28. 2
29. 1
30. 3
31. 1
32. 3
33. 3
34. 1
35. 2
36. 1
37. 4
38. 4
39. 3
40. 1
41. 4,5
JavaBeat 2005, India (www.javabeat.net)
Submit a Site - Directory - Submit Articles