Java Test

Congratulations - you have completed Java Test. You scored %%SCORE%% out of %%TOTAL%%. Your performance has been rated as %%RATING%%
Your answers are highlighted below.
Question 1
Which of the following lines will compile without warning or error.
A
1) float f=1.3;
B
2) char c='a';
C
3) byte b=257;
D
4) boolean b=null;
E
5) int i=10;
Question 2

What will happen if you try to compile and run the following code ?

public class MyClass {
 public static void main(String arguments[]) {
  amethod(arguments);
 }
 public void amethod(String[] arguments) {
  System.out.println(arguments);
  System.out.println(arguments[1]);
 }
}
A
error Can't make static reference to void amethod.
B
error method main not correct
C
error array must include parameter
D
amethod must be declared with String
Question 2 Explanation: 
Can't make static reference to void amethod. Because main is defined as static you need to create an instance of the class in order to call any non-static methods. Thus a typical way to do this would be. MyClass m=new MyClass(); m.amethod(); Answer 2 is an attempt to confuse because the convention is for a main method to be in the form String argv[] That argv is just a convention and any acceptable identifier for a string array can be used. Answers 3 and 4 are just nonsense
Question 3
A byte can be of what size
A
-128 to 127
B
(-2 power 8 )-1 to 2 power 8
C
-255 to 256
D
depends on the particular implementation of the Java Virtual machine
Question 4
What will be printed out if this code is run with the following command line?
java myprog good morning
public class myprog{
  public static void main(String argv[])
  {
     System.out.println(argv[2]);
  }
}
A
myprog
B
good
C
morning
D
Exception raised: 'java.lang.ArrayIndexOutOfBoundsException: 2'
Question 4 Explanation: 
Unlike C/C++ java does not start the parameter count with the program name. It does however start from zero. So in this case zero starts with good, morning would be 1 and there is no parameter 2 so an exception is raised
Question 5
Which of the following is NOT a keywords or reserved words in Java?
A
if
B
then
C
goto
D
while
E
case
Question 6
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]);
 }
}
A
1
B
Error anar is referenced before it is initialized
C
2
D
Error: size of array must be defined
Question 6 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.
Question 7
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");
}
A
default
B
default, zero
C
error default clause not defined
D
no output displayed
Question 7 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 8
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;
    }
}
A
Compile and run without error
B
Compile time Exception
C
Runtime Exception
Question 8 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)
Question 9
What will happen when you attempt to compile and run the following code?
public class Tux extends Thread{

        static String sName = "vandeleur";
        public static void main(String argv[]){
        Tux t = new Tux();
        t.piggy(sName);
        System.out.println(sName);
        
        }
        public void piggy(String sName){
                sName = sName + " wiggy";
        start();
        }
        public void run(){
        
        for(int i=0;i  <  4; i++){
                sName = sName + " " + i;
                
        }
        }

}
A
Compile time error
B
Compilation and output of 'vandeleur wiggy'
C
Compilation and output of 'vandeleur wiggy 0 1 2 3'
D
Compilation and output of either 'vandeleur', 'vandeleur 0', 'vandeleur 0 1' 'vandaleur 0 1 2' or 'vandaleur 0 1 2 3'
Question 9 Explanation: 
If that seems a vauge answer it is because you cannot be certain of the system that the underlying OS uses for allocating cycles for a Thread. The chances are that once the thread has been spun off in the call to start in the method piggy the main method will run to completion and the value of sName will still be vandeluer before the Thread modifies it. You cannot be certain of this though.
Question 10
What will be output by the following code?
public class MyFor{
    public static void main(String argv[]){
	int i;
	int j;
    outer:
	for (i=1;i <3;i++)
	    inner:
	for(j=1; j<3; j++) {
	    if (j==2)
		continue outer;
	    System.out.println("Value for i=" + i + " Value for j=" +j);
	}
    }

}
A
Value for i=1 Value for j=1
B
Value for i=3 Value for j=1
C
Value for i=2 Value for j=2
Once you are finished, click the button below. Any items you have not completed will be marked incorrect. Get Results
There are 10 questions to complete.
List
Return
Shaded items are complete.
12345
678910
End
Return