Exercise: Objects And Collections

Questions for: Objects And Collections

What will be the output of the program?
public class Test 
{ 
    public static void main (String args[]) 
    {
        String str = NULL; 
        System.out.println(str); 
    } 
}
A:
NULL
B:
Compile Error
C:
Code runs but no output
D:
Runtime Exception
Answer: B

Option B is correct because to set the value of a String variable to null you must use "null" and not "NULL".

What will be the output of the program?
public class Test 
{ 
    public static void main (String[] args) 
    {
        String foo = args[1]; 
        String bar = args[2]; 
        String baz = args[3]; 
        System.out.println("baz = " + baz); /* Line 8 */
    } 
}

And the command line invocation:

> java Test red green blue

A:
baz =
B:
baz = null
C:
baz = blue
D:
Runtime Exception
Answer: D

When running the program you entered 3 arguments "red", "green" and "blue". When dealing with arrays in java you must remember ALL ARRAYS IN JAVA ARE ZERO BASED therefore args[0] becomes "red", args[1] becomes "green" and args[2] becomes "blue".

When the program entcounters line 8 above at runtime it looks for args[3] which has never been created therefore you get an

ArrayIndexOutOfBoundsException at runtime.

Which of the following are Java reserved words?
  1. run
  2. import
  3. default
  4. implement
A:
1 and 2
B:
2 and 3
C:
3 and 4
D:
2 and 4
Answer: B

(2) - This is a Java keyword

(3) - This is a Java keyword

(1) - Is incorrect because although it is a method of Thread/Runnable it is not a keyword

(4) - This is not a Java keyword the keyword is implements

What is the numerical range of char?
A:
0 to 32767
B:
0 to 65535
C:
-256 to 255
D:
-32768 to 32767
Answer: B

The char type is integral but unsigned. The range of a variable of type char is from 0 to 216-1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of representing a wide range of international characters. If the most significant nine bits of a char are 0, then the encoding is the same as seven-bit ASCII.

/* Missing Statement ? */
public class foo 
{
    public static void main(String[]args)throws Exception 
    {
        java.io.PrintWriter out = new java.io.PrintWriter(); 
        new java.io.OutputStreamWriter(System.out,true); 
        out.println("Hello"); 
    } 
}
What line of code should replace the missing statement to make this program compile?
A:
No statement required.
B:
import java.io.*;
C:
include java.io.*;
D:
import java.io.PrintWriter;
Answer: A

The usual method for using/importing the java packages/classes is by using an import statement at the top of your code. However it is possible to explicitly import the specific class that you want to use as you use it which is shown in the code above. The disadvantage of this however is that every time you create a new object you will have to use the class path in the case "java.io" then the class name in the long run leading to a lot more typing.

Ad Slot (Above Pagination)
Quiz