Question 1.
Read the code below carefully.
if( "String".endsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
The code produces
- True
- False
Question 2.
Read the code below carefully.
if( "String".startsWith(""))
Sytem.out.println("True");
else
System.out.println("False");
The code produces
- True
- False
Question 3.
Read the code below carefully.
public class TestClass
{
public static void main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
if(sb1.equals(sb2))
{
//lots of code
}
}
}
Is the line marked "lots of code" ever reached?
Question 4.
Read the code below carefully.
public class NiceThreads implements Runnable
{
public void run()
{
while(true)
{
}
}
public static void main(String args[])
{
NiceThreads nt1 = new NiceThreads();
NiceThreads nt2 = new NiceThreads();
NiceThreads nt3 = new NiceThreads();
nt1.run();
nt2.run();
nt3.run();
}
}
- The code does not compile - "nt2.run() is never reached"
- The code compiles and runs 3 non ending non demon threads.
- The code compiles but runs only 1 non ending, non demon thread.
Question 5.
When does the JVM exit?
- After the main method returns.
- After all the non demons threads created by the application complete.
- After all the demon threads created by the application complete
- When a thread executes System.exit();
- When an uncaught exception is thrown in a non demon thread.
- When an uncaught exception is thrown in a demon thread.
Question 6.
Read the following code, which is a part of a synchronized method of a monitor.
public synchronized void someMethod()
{
//lots of code
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
//do some crap here.
}
//more and more code here
}
- The code causes compilation error - sleep cannot be called inside synchronized methods.
- The code causes compilation error - sleep is not a static method of java.lang.Thread
- The Thread sleeps for at least 500 milliseconds in this method if not interrupted.
- When the thread "goes to sleep" it releases the lock on the object.
- 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
- is always public
- is always "friendly"
- always defaults to the access modifier provided for the class.
- depends on the compilation options of javac
Question 8.
Which of the following is the direct base class of java.awt.AWTEvent.
- java.lang.Object.
- java.util.EventObect
Question 9.
Interface methods can be declared with the following modifiers
- public
- none (i.e., no access modifier).
- private.
- static
- native
- synchronized.
Question 10.
Which of the following are true about the class defined inside an interface
- it is not possible in the java Laungage.
- The class is always public.
- The class is always static.
- the class methods cannot call the methods declared in the interface.
- the class methods can call only the static methods declared in the interface.
Question 11.
what does the following expression return?
(0.0 == -0.0)
- true
- false
Question 12.
What does the following expression print on the screen?
System.out.println(Math.min(Float.NaN, Float.POSITIVE_INFINITY));
- NaN
- Infinity
- The expression throws a runtime exception - NaN is an illegal argument.
Question 13.
What does the following expression return
Math.max(Float.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
- Float.POSITIVE_INFINITY
- Double.POSITIVE_INFINITY
- runtime Exception
Question 614.
Read the following code carefully.
public class AStringQuestion
{
static String s1;
static String s2;
public static void main(String args[])
{
s2 = s1+s2;
System.out.println(s2);
}
}
Attempting to compile and run the code
- Will cause a compilation error.
- Runtime Execption - NullPointerException in the 2nd line of the main method.
- Will compile successfully and print nullnull on the screen.
- Will compile successfully and print an empty line on the screen.
- Will compile successfully and print nothing on the screen.
Question 15.
Read the code below carefully.
public class AQueryClass
{
public static synchronized void method1()
{
//lots and lots of code here.
}
public sychronized void method2()
{
//lots of code here too.
}
}
Assuming that all the code inside the method is legal and cannot be a cause for compilation error -
- An attempt to compile will cause a compilation error. Static methods cannot be synchronized.
- Compilation will be successfull. The Object instantiated will have a lock which has to be gained by Threads calling any of the two methods.
- 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.
- 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.
Class fields with the following modifiers will not be serialized
- private
- static
- transient
- protected
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
c:\somedirectory> java Cat
An expression in the main method is as follows
System.out.println(args.length);
- The above expression will cause a '0' appear on the command line.
- Will throw a NullPointerException.
- Will a blank line to appear.
Question 18.
A demon Thread group
- has only demon threads.
- can have non demon threads.
- does not exist after all non demon threads in the group have finished executing.
- does not exist after all the threads in the group have finished executing.
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.
- True
- False
Question 20.
Read the code below carefully.
import java.io.*;
public class OutOut
{
public static void main(String args[]) throws IOException
{
PrintStream pr = new PrintStream(new FileOutputStream("outfile"));
System.out = pr;
System.out.println("Lets see what I see now??");
}
}
- The code causes a compiler error. out is a declared final in System and cannot be assigned to pr.
- The code causes a runtime Exception due the assignment to a final variable.
- 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.