Java Quiz
Question 11.
public class AQuestion
{
private int
i
= giveMeJ();
private int
j
= 10;
private
int giveMeJ()
{
return j;
}
public
static void
main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers
-
Compiler error complaining about access restriction of private
variables
of AQuestion.
-
Compiler error complaining about forward referencing.
-
No Compilation error - The output is 0;
-
No Compilation error - The output is 10;
Question 12.
public class AQuestion
{
public
static
void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
-
Compiler error complaining about the catch block, where no Throwable
object
can ever be thrown.
-
Compiler error - Throwable Object can not be caught, only Exceptions
must
be caught.
-
No compiler error. The lines "Before Try" and "At the end" are printed
on the screen.
Question 13.
public class AQuestion
{
public static void main(String
args[])
{
System.out.println("Before
Try");
try
{
}
catch(java.io.IOException
t)
{
System.out.println("Inside Catch");
}
System.out.println("At
the End");
}
}
-
Compiler error complaining about the catch block where no IOException
object
can ever be thrown.
-
Compiler error - IOException not found. It must be imported in the
first
line of the code.
-
No compiler error. The lines "Before Try" and "At the end" are printed
on the screen.
Question 14.
The class
java.lang.Exception
i.
Is public
ii.
Extends Throwable
iii.
Implements Throwable
iv.
Is serializable
Question 15.
Read this
piece of
code carefully
if("String".trim() == "String".trim())
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 16.
Read this
piece of
code carefully
if( "STRING".toUpperCase() == "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 17.
The following lines
of code
byte b = 0;
b += 1;
-
results in b having the value 1.
-
causes a compiler error.
-
will require a cast (byte) before 1.
Question 18.
The following
express
char
c = -1;
-
will cause a compiler error as the range of
character is between
0 and 2^16 - 1. Will request for an explicit cast.
-
will not cause a compiler error and c will have the
value -1;
-
c will not represent any ascii character.
-
c will still be a unicode character.
Question 19.
Which of the
following statements
are true?
-
A method can throw an Exception
-
A method can return an Exception
Question 20.
All the
wrapper classes
(Integer, Boolean, Float, Short, Long, Double and Character)
-
are public
-
are serializable
-
are immutatable
-
extend java.lang.Number
-
are final
If
you have any concerns regarding any of the questions in this page,
please
mail me NOW!
|