Public Class Public Static Void Int For Int: Chapter 1: Language Fundamentals Java Programming Language Keywords
Public Class Public Static Void Int For Int: Chapter 1: Language Fundamentals Java Programming Language Keywords
ANSWER: C
The word “signed” is not a valid modifier keyword in the Java language. All number primitives
in Java are signed. Always.
A. method
B. native
C. subclasses
D. reference
E. array
ANSWER: B
A, D, and E are not keywords. C is wrong because the keyword for subclassing in Java is extends,
not ‘subclasses’.
3. Which one of these lists contains only Java programming language keywords? (Choose one.)
ANSWER: B
A. interface
B. unsigned
C. Float
D. this
E. string
ANSWER: A and D
B is wrong because “unsigned” is a keyword in C/C++ but not in Java. C is wrong because “Float”
is a class type. The keyword for the Java primitive is float. E is wrong because although “String”
is a class type in Java, “string” is not a keyword.
ANSWER: A, C, and F
A is an octal representation of the integer value 27128, which is legal because it fits into an
unsigned 16-bit integer. C is a hexadecimal representation of the integer value48879, which fits
into an unsigned 16-bit integer. F is a Unicode representation of a character.
B is wrong because you can’t put more than one character in a char literal. You know that B is a
literal character because it comes between single quotes. The only other acceptable char literal
that can go between single quotes is a Unicode value, and Unicode literals must always start
with a ‘\u’. D is wrong because the single quotes are missing. E is wrong because it appears to
be a Unicode representation (notice the backslash), but starts with ‘\i’ rather than ‘\u’.
A. String s1 = null;
B. String s2 = ‘null’;
C. String s3 = (String) ‘abc’;
D. String s4 = (String) ‘\ufeed’;
E. String s5 = “strings rule”;
A sets the String reference to null; E initializes the String reference with a literal.
B is wrong because null cannot be in single quotes. C is wrong because there are multiple
characters between the single quotes (‘abc’). D is wrong because you can’t cast a char
(primitive) to a String (object).
ANSWER: C
ANSWER: E
A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.
ANSWER: A, C, and F.
A and C are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits).
F is correct because F is appended to the literal, declaring it as a float rather than a double (the
default for floating point literals).
ANSWER: A, B, and D
With an array declaration, you can place the brackets to the right or left of the identifier. A looks
strange, but it’s perfectly legal to split the brackets in a multidimensional array, and place them
on both sides of the identifier. Although coding this way would only annoy your fellow
programmers, for the exam, you need to know it’s legal.
C and E are wrong because you can’t declare an array with a size. The size is only needed when
the array is actually instantiated (and the JVM needs to know how much space to allocate for
the array, based on the type of array and the size).
A. 7
B. 9
C. 11
D. 13
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: C
Which of the following lines of code could be inserted at line 7, and still allow the code to
compile? (Choose four that would work.)
A. b2[0][1] = b;
B. b[0][0] = b3;
C. b2[1][1][0] = b[0][0];
D. b2[1][2][0] = b;
E. b2[0][1][0][0] = b[0][0];
F. b2[0][1] = big;
ANSWER: A, B, E, and F
This question covers the issue of, “What can I assign to an array reference variable?” The key is
to get the dimensions right. For example, if an array is declared as a two-dimensional array, you
can’t assign a one-dimensional array to a one-dimensional array reference.
C is wrong because it tries to assign a primitive byte where a byte array (one dimension) is
expected. D is wrong because it tries to assign a two-dimensional array where a one-
dimensional array is expected.
13. Which two will declare an array and initialize it with five numbers? (Choose two.)
ANSWER: B and D
Both are legal ways to declare and initialize an array with five elements.
14. Which will legally declare, construct, and initialize an array? (Choose one.)
ANSWER: D
A is wrong because it initializes an int array with String literals. B and E are wrong because they
use something other than curly braces for the initialization. C is wrong because it provides
initial values for only one dimension, although the declared array is a two-dimensional array. F
is wrong because it uses semicolons where it should use commas, to separate the items in the
initialization.
15. Which four describe the correct default values for array elements of the types indicated?
(Choose four.)
A. int -> 0
B. String -> “null”
C. Dog -> null
D. char -> ‘\u0000’
E. float -> 0.0f
F. boolean -> true
ANSWER: A, C, D, and E
B is wrong because the default value for a String (and any other object reference) is null, with
no quotes. F is wrong because the default value for boolean elements is false.
class Dog {
}
A. null
B. theDogs
C. Compilation fails
D. An exception is thrown at runtime
ANSWER: D.
The second dimension of the array referenced by theDogs has not been initialized. Attempting
to access an uninitialized object element (line 4) raises a NullPointerException.
public class X {
public static void main(String[] args) {
String names[] = new String[5];
for (int x = 0; x < args.length; x++)
names[x] = args[x];
System.out.println(names[2]);
}
}
java X a b
A. names
B. null
C. Compilation fails
D. An exception is thrown at runtime
ANSWER: B
The names array is initialized with five null elements. Then elements 0 and 1 are assigned the
String values “a” and “b” respectively (the command-line arguments passed to main). Elements
2, 3, and 4 remain unassigned, so they have a value of null.
java CommandArgs 1 2 3 4
A. args[2] = 2
B. args[2] = 3
C. args[2] = null
D. args[2] = 1
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: F.
An exception is thrown because at line 6, the array index (the fifth element) is out of bounds.
The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.
java CommandArgsTwo 1 2 3
ANSWER: F
An exception is thrown because at some point in line 7, the value of x will be equal to y, resulting
in an attempt to access an index out of bounds for the array. Remember that you can access only
as far as length-1, so loop logical tests should use x<someArray.length as opposed to x <=
someArray.length.
java CommandArgsThree 1 2 3
A. 00
B. 12
C. 000
D. 123
E. Compilation fails
F. An exception is thrown at runtime
ANSWER: D.
In line 5, the reference variable argCopy[0], which was referring to an array with two elements,
is reassigned to an array (args) with three elements.