Which code fragments will print the last argument given on the command line to the
standard output, and exit without any output and exceptions if no arguments are given?
1.
public static void main(String args[ ])
{
if (args.length != 0) System.out.println(args[args.length-1]);
}
2.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (ArrayIndexOutOfBoundsException e) { }
}
3.
public static void main(String args[ ])
{
int i = args.length;
if (i != 0) System.out.println(args[i-1]);
}
4.
public static void main(String args[ ])
{
int i = args.length-1;
if (i > 0) System.out.println(args[i]);
}
5.
public static void main(String args[ ])
{
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}
Question 3 of 61
Question 4 of 61
Question 7 of 61
Question 8 of 61
Question 9 of 61
Question 11 of 61
Question 12 of 61
Question 13 of 61
Question 14 of 61
What will the following code print when compiled and run?
class Base
{
void methodA()
{
System.out.println("base - MethodA");
}
}
class Sub extends Base
{
public void methodA()
{
System.out.println("sub - MethodA");
}
public void methodB()
{
System.out.println("sub - MethodB");
}
public static void main(String args[])
{
Base b=new Sub(); //1
b.methodA(); //2
b.methodB(); //3
}
}
What will happen when you compile and run the following program using the command
line:
java TestClass 1 2
public class TestClass
{
public static void main(String[] args)
{
int i = Integer.parseInt(args[1]);
System.out.println(args[i]);
}
}
Question 18 of 61
Question 20 of 61
What will be the result of compiling and running the following code :
public class TestClass
{
public static void main (String args[ ] )
{
int k = 1;
int i = ++k + k++ + + k ;
System.out.print(i+ " "+ k) ;
}
}
Question 21 of 61
Question 22 of 61
Question 23 of 61
Question 25 of 61
Which of the following can be used as a constructor for the class shell given below?
public class TestClass
{
// lots of code ...
}
Which variables of the encapsulationg class, can an inner class access if the inner class
is defined in an instance method of the encapsulating class?
Question 29 of 61
What is wrong with the following code written in a single file named TestClass.java?
class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }
public class TestClass
{
public static void main(String args[]) throws SomeThrowable
{
try{
m1();
}catch(SomeThrowable e){
throw e;
}finally{
System.out.println("Done");
}
}
public static void m1() throws MyThrowable
{
throw new MyThrowable();
}
}
Question 31 of 61
Which of the following constructors MUST exist in SuperClass for SubClass to compile
correctly?
Question 33 of 61
You are designing a class that will cache objects. This class should work by tracking the
"last accessed times" of the objects.
Which collection class would you use to store the objects?
Question 35 of 61
Question 36 of 61
Question 37 of 61
A method is ..
What will be the result of attempting to compile and run the following class?
public class InitTest
{
static String s1 = sM1("a");
{
s1 = sM1("b");
}
static
{
s1 = sM1("c");
}
public static void main(String args[])
{
InitTest it = new InitTest();
}
private static String sM1(String s)
{
System.out.println(s); return s;
}
}
Question 39 of 61
Question 40 of 61
You want to write another class CleanConnector that extends from PortConnector. Which
of the following statements should hold true for CleanConector class?
Question 42 of 61
Question 43 of 61
What is the return type of method round(float) from the Math class?
Question 45 of 61
Question 46 of 61
Which of these statements concerning the use of standard collection interfaces are true?
Question 48 of 61
You want to run Main.class from the command line. It uses two packages good.* and
bad.*. You want to enable assertions for all classes of bad.* and at the same time want to
disable them for the package good.*.
Which of the following command lines will achieve the above?
Question 49 of 61
return ?
Question 50 of 61
Question 52 of 61
Question 53 of 61
Assume that Thread 1 currently holds the lock for an object (obj) for which 4 other
threads, Thread 2 to 5, are waiting. Now, Thread 1 want to release the lock but as the
same time, it want Thread 3 to get the lock. How will you accomplish this?
Question 55 of 61
Question 58 of 61
You want to run Main.class [Assume that it belongs to the default package.] from the
command line. It uses two packages good.* and bad.*. You want to disable assersions for
all classes of bad.* as well as good.* but at the same time want to enable them for
Main.class.
Which of the following command lines will achieve the above?
Which of these statements concerning the use of standard collection interfaces are true?
Question 60 of 61
Question 61 of 61