Navigation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topic: Declarations and access control
Question #21 |
|
What will happen when you compile and run the following code? |
public class MyClass
{
static int i;
public static void main(String argv[])
{
System.out.println(i);
}
}
|
- Error Variable i may not have been initialized
- null
- 1
- 0
|
Click here to see the answer
|
The Correct Answer is 4: 0
|
Click here to see explanation
|
Class level variables are always initialised to default values. In the case of an int this will be 0. Method level variables are not given default values and if you attempt to use one before it has been initialised it will cause the
error.
|
Question #22 |
|
What will happen if you try to compile and run the following code? |
public class Q
{
public static void main(String argv[])
{
int anar[]=new int[]{1,2,3};
System.out.println(anar[1]);
}
}
|
- 1
- Error anar is referenced before it is initialized
- 2
- Error: size of array must be defined
|
Click here to see the answer
|
The Correct Answer is 3: 2
|
Click here to see explanation
|
No error will be triggered.
Like in C/C++, arrays are always referenced from 0. Java allows an array to be populated at creation time. The size of array is taken from the number of initializers. If you put a size within any of the square brackets you will get an error.
|
Question #23 |
|
What will happen if you try to compile and run the following code? |
public class Q
{
public static void main(String argv[])
{
int anar[]=new int[5];
System.out.println(anar[0]);
}
}
|
- Error: anar is referenced before it is initialized
- null
- 0
- 5
|
Click here to see the answer
|
The Correct Answer is 3: 0
|
Click here to see explanation
|
Arrays are always initialised when they are created. As this is an array of ints it will be initalised with zeros
|
Question #24 |
|
What will be the result of attempting to compile and run the following code? |
abstract class MineBase
{
abstract void amethod();
static int i;
}
public class Mine extends MineBase
{
public static void main(String argv[])
{
int[] ar=new int[5];
for(i=0;i < ar.length;i++)
System.out.println(ar[i]);
}
}
|
- A sequence of 5 0's will be printed
- Error: ar is used before it is initialized
- Error Mine must be declared abstract
- IndexOutOfBoundes Error
|
Click here to see the answer
|
The Correct Answer is 3: Error Mine must be declared abstract
|
Click here to see explanation
|
A class that contains an abstract method must itself be declared as abstract. It may however contain non abstract methods. Any class derived from an abstract class must either define all of the abstract methods or be declared abstract itself
|
Question #25 |
|
What will be printed out if you attempt to compile and run the following code ? |
int i=1;
switch (i)
{
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
default:
System.out.println("default");
}
|
- one
- one, default
- one, two, default
- default
|
Click here to see the answer
|
The Correct Answer is 3: one, two, default
|
Click here to see explanation
|
Code will continue to fall through a case statement until it encounters a break.
|
Question #26 |
|
What will be printed out if you attempt to compile and run the following code? |
int i=9;
switch (i)
{
default:
System.out.println("default");
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
}
|
- default
- default, zero
- Error: default clause not defined
- No output displayed
|
Click here to see the answer
|
The Correct Answer is 2: default, zero
|
Click here to see explanation
|
Although it is normally placed last the default statement does not have to be the last item as you fall through the case block. Because there is no case label found matching the expression the default label is executed and the code continues to fall through until it encounters a break.
|
Question #27 |
|
Which of the following lines of code will compile without error? |
- int i=0;
if(i)
{
System.out.println("Hello");
}
- boolean b=true;
boolean b2=true;
if(b==b2)
{
System.out.println("So true");
}
- int i=1;
int j=2;
if(i==1|| j==2)
System.out.println("OK");
- int i=1;
int j=2;
if(i==1 &| j==2)
System.out.println("OK");
|
Click here to see the answer
|
The Correct Answer is 2: boolean b=true;
boolean b2=true;
if(b==b2)
{
System.out.println("So true");
} 3: int i=1;
int j=2;
if(i==1|| j==2)
System.out.println("OK");
|
Click here to see explanation
|
Example 1 will not compile because if must always test a boolean. This can catch out C/C++ programmers who expect the test to be for either 0 or not 0.
|
Question #28 |
|
What will be output if you try to compile and run the following code, but there is
no file called Hello.txt in the current directory? |
import java.io.*;
public class Mine
{
public static void main(String argv[])
{
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod()
{
try
{
FileInputStream dis=new FileInputStream("Hello.txt");
}
catch (FileNotFoundException fne)
{
System.out.println("No such file found");
return -1;
}
catch(IOException ioe)
{
}
finally
{
System.out.println("Doing finally");
}
return 0;
}
}
|
- No such file found
- No such file found ,-1
- No such file found, Doing finally, -1
- 0
|
Click here to see the answer
|
The Correct Answer is 3: No such file found, Doing finally, -1
|
Click here to see explanation
|
The no such file found message is to be expected, however you can get caught out if you are not aware that the finally clause is almost always executed, even if there is a return statement.
|
Question #29 |
|
Which of the following statements are true? |
- Methods cannot be overridden to be more private.
- Static methods cannot be overloaded.
- Private methods cannot be overloaded.
- An overloaded method cannot throw exceptions not checked in the base class.
|
Click here to see the answer
|
The Correct Answer is 1: Methods cannot be overridden to be more private.
|
Click here to see explanation
|
Static methods cannot be overriden but they can be overloaded. If you have doubts about that statement, please follow and read carefully the link given to the Sun tutorial below. There is no logic or reason why private methods should not be overloaded. Option 4 is a jumbled up version of the limitations of exceptions for overriden methods.
|
Question #30 |
|
What will happen if you attempt to compile and run the following code? |
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class Cex
{
public static void main(String argv[])
{
Base b=new Base();
Sub s=(Sub) b;
}
}
|
- Compile and run without error
- Compile time Exception
- Runtime Exception
|
Click here to see the answer
|
The Correct Answer is 3: Runtime Exception
|
Click here to see explanation
|
Without the cast to sub you would get a compile time error. The cast tells the compiler that you really mean to do this and the actual type of b does not get resolved until runtime. Casting down the object hierarchy is a problem, as the compiler cannot be sure what has been implemented in descendent classes. Casting up is not a problem because sub classes will have the features of the base classes. This can feel counter intuitive if you are aware that with primitives casting is allowed for widening operations (ie byte to int).
|
|
|
|
|
|
|
|
Today, there have been 1 visitors (3 hits) on this page! |
|
|
|
|
|
|
|