|
Java Quiz
Question 21.
Read this piece of
code carefully
if("String".substring(0) == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
-
the code will compile an print "Equal".
-
the code will compile an print "Not Equal".
-
the code will cause a compiler error
Question 22.
Read this piece of
code carefully
if("String".substring(0,6) == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
-
the code will compile an print "Equal".
-
the code will compile an print "Not Equal".
-
the code will cause a compiler error
Question 23.
Read this piece of
code carefully
if("String".replace('t','t') == "String")
System.out.println("Equal");
else
System.out.println("Not Equal");
Answers
-
the code will compile an print "Equal".
-
the code will compile an print "Not Equal".
-
the code will cause a compiler error
Question 24.
An Anonymous Inner
class
-
Does not have a constructor
-
Can implement an interface
-
Can extend a non-final Class
-
Can implement an interface and extend a non-final class
(at the same time).
Question 25.
public class A
{
private void method1() throws Exception
{
throw new RuntimeException();
}
public void method2()
{
try
{
method1();
}
catch(RuntimeException e)
{
System.out.println("Caught
Runtime Exception");
}
catch(Exception e)
{
System.out.println("Caught
Exception");
}
}
public static void main(String args[])
{
A a = new A();
a.method2();
}
}
The above lines of code -
-
will not compile.
-
will compile and show - "Caught Runtime Exception".
-
will compile and show - "Caught Exception".
-
will compile and show both the messages one after another
in the order they appear.
Question 26.
public XXXX extends something1, something2
-
XXX should be an interface,something1 and something2 need not,
for the expression to be legal
-
XXX should be a class, something1 and something2 must be interfaces
for the expression to be legal.
-
XXX, something1 and something2 must be interfaces for the expression
to be legal.
-
The above statement is alway illegal in Java as multiple inheritance
is not supported.
Question 27.
public class ADirtyOne
{
char a = '\u000A';
}
An attempt to compile the above class
-
will complete successfully.
-
will not compile as 0x000A is out of range for unicode charaters.
-
will complain about illegal character assignment
-
will compile but will cause a runtime error in accessing
the variable.
Question 28.
public class ADirtyOne
{
//char a = '\u000A';
}
An attempt to compile the above class
-
will complete successfully.
-
will compile sucessfully but with a warning message.
-
will not compile - complains on an invalid expression.
Question 29.
public class AnotherDirtyOne
{
private final int i =10;
private byte k = i;
}
An attempt to compile and run the above code will
-
Cause a compilation error due to invalid assignment ( int
to byte) and will request for an explicit cast to be done on
i [ k=(byte) i ].
-
Compilation occurs with a warning message - suggesting that the
accuracy of k is questionable
-
Compilation will occur cleanly without any warning message.
-
Runtime error occurs when accessing k.
Question 30.
interface One
{
public void someMethod();
}
public class One_impl implements One
{
public native void someMethod();
}
Assuming that the native method is not provided in any local
library, an attempt to compile and run the above lines of code will cause
-
Compilation error - implimentations can never be native.
-
Compilation error - method not found in local libraries.
-
Runtime Exception - method not found in local libraries.
-
Compilation successfull but runtime error is thrown if and only
if the method someMethod of class One_impl is called.
If
you have any concerns regarding any of the questions in this page, please
mail me NOW!
|