Page 1:
SCJP questions 1-20
Page 2: SCJP questions 21-40
- What is the output of the following code ?
Long longL1 = new Long(5);
int intI1 = 5;
if (longL1.equals(intI1)) System.out.println("longL1 is equal with intI1");
\/\/\/\/\/\/\/\/\/\/
The code does not compile: all equals() methods are taking Object references as parameter.
- What is the output of the following code ?
public void someMethod()
{
int arr[];
arr[0] = 23;
System.out.println(arr[0]);
}
\/\/\/\/\/\/\/\/\/\/
The code does NOT compile ! The array must be initialized after declaration:
int arr[] = new int [10];
- What is the output of the following code ?
public static void printResult(Integer arr[])
{
if (arr instanceof Float[]) System.out.println("arr is instance of Float[]");
}
\/\/\/\/\/\/\/\/\/\/
The code does not compile ! Since the reference 'arr' can never point to an instance type
Float[] the compiler flags this as an error. OTOH a reference of type Object can point to an object
of type Float[]:
public static void printResult(Object obj)
{
if (obj instanceof Float[]) System.out.println("obj is instance of Float[]");
}
- What is wrong with the following code ?
int []fontSize = {9, 11, 17, 21};
String [][] fintDesc = { {"TimesNewRoman", "bold"}, {"Courier", "italic"}, {"DingBats", "normal"}};
\/\/\/\/\/\/\/\/\/\/
Nothing ! An array can be directly initialized this way.
- Does the following code compile ?
char c = 4096;
\/\/\/\/\/\/\/\/\/\/
Yes ! 4096 fits in a char (the compiler is smart enough to see that the integer 4096 will fit in a char; an
automatic conversion from int to char is performed)
- What is the output of the following code ?
char ch = 'c';
switch(ch)
{
case 1: System.out.println("1");break;
case -1: System.out.println("-1");break;
default: System.out.println("default");break;
}
\/\/\/\/\/\/\/\/\/\/
The code does not compile ! (the constant -1 is out of the range of char)
- What is the output of the following code ?
long ll = 100;
switch(ll)
{
case 1: System.out.println("1");break;
case -1: System.out.println("-1");break;
default: System.out.println("default");break;
}
\/\/\/\/\/\/\/\/\/\/
The code does not compile ! Only integers or types that can be promoted to an integer
("byte", "char" and "short") can appear in a switch statement.
- What is the output of the following code ?
Integer ii = new Integer(23);
switch(ii)
{
case 1: System.out.println("1");break;
case -1: System.out.println("-1");break;
default: System.out.println("default");break;
}
\/\/\/\/\/\/\/\/\/\/
The code does not compile ! The Integer class cannot be used instead of primary type int.
- In Java there are 4 types of datatypes: primitives, classes, interfaces and arrays. True/False
\/\/\/\/\/\/\/\/\/\/
Answer: True.
- Java provides 4 primitive data types: characters, integers, floating and boolean.
\/\/\/\/\/\/\/\/\/\/
Answer: Yes.
- How many bits are used to store a boolean value ?
\/\/\/\/\/\/\/\/\/\/
The number of bits is system-dependent. Java guarantees that a boolean can store true
or false values but it does not specify how many bits will be used to actually store that info.
- Can you compile and run the following code ?
public class SimpleJava
{
static void main(String args[])
{
System.out.println("SimpleJava ....");
}
}
\/\/\/\/\/\/\/\/\/\/
You can compile but you cannot run. (the main method is not public)
- Which statement is true about the following code?
public class CDummy
{
public static void main(String argv[])
{
String str1 = new String("test1");
StringBuffer str2 = new StringBuffer("test2");
printIt(str1);
printIt(str2);
}
public static void printIt(String s)
{
System.out.println(s);
}
}
- a. It will print "test1" followed by "test2";
- b. It will print "test1" then an exception will be thrown;
- c. This code will not compile;
\/\/\/\/\/\/\/\/\/\/
This code will not compile. An instance of StringBuffer cannot be passed to a method that
is expecting an instance of String.
- What is the output of the following code ?
public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy...");
try {
doIt();
}
catch (RuntimeException ee) {;}
catch (ArithmeticException ee) {;}
catch (Exception ee) {;}
}
static void doIt() throws ArithmeticException, RuntimeException, Exception
{
System.out.println("DoIt!");
}
}
\/\/\/\/\/\/\/\/\/\/
The code does not compile ! The catch exception order is wrong. The exception order should be:
catch (ArithmeticException ee) {;}
catch (RuntimeException ee) {;}
catch (Exception ee) {;}
- What is the output of the following code ?
public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy...");
doIt();
}
static public doIt()
{
System.out.println("DoIt!");
}
}
\/\/\/\/\/\/\/\/\/\/
The code does not compile ! The function doIt() must have a return type.
- If the System.exit() function is called in a catch block, will the finally block be executed ?
\/\/\/\/\/\/\/\/\/\/
No ! The current thread will instantly finish.
- If the return() function is called in a catch block, will the finally block be executed ?
\/\/\/\/\/\/\/\/\/\/
Yes !
- Will the following code compile ?
static int doIt() throws ArithmeticException, RuntimeException, Exception
{
System.out.println("DoIt!");
throw new ArithmeticException();
return 7;
}
\/\/\/\/\/\/\/\/\/\/
No ! The return statement is unreachable.
- What is the output of the following program ?
public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy...");
System.out.println("textX() = " + testX());
}
public static int testX() {
try {
return 1;
}catch(ArithmeticException aex) {
return 2 ;
}catch(Exception ex) {
return 3;
}finally {
return 4;
}
}
}
\/\/\/\/\/\/\/\/\/\/
The output is 4. Because the finally clause is calling return
there is a warning, because the return code 1 (from the try block) is
discarded.
- What is the output of the following program ?
public class CDummy
{
public static void main(String args[])
{
System.out.println("CDummy...");
System.out.println("textX() = " + testX());
}
public static int testX() {
try {
throw new ArithmeticException();
}catch(ArithmeticException aex) {
return 2 ;
}catch(Exception ex) {
return 3;
}finally {
return 4;
}
}
}
\/\/\/\/\/\/\/\/\/\/
The output is 4. Because the finally clause is calling return there is a warning,
because the return code 2 (from the catch block) is discarded.