Free Sun Certified Java Programmer Questions and Answers — Questions and Answers
Question 1: Which of the following accurately describes the main() method of an application?
- public static void main( String args );
- public static void main();
- public static void main( Graphics g );
- public static void main( String args[] ); (Correct answer)
Correct answer: public static void main( String args[] );
In Java, the `main` method serves as the entry point for any standalone application. Its signature must strictly be `public static void main(String[] args)`. `public` allows universal access, `static` enables calling without an object, `void` signifies no return value, and `String[] args` is an array for command-line arguments. Any deviation from this specific signature will prevent the Java Virtual Machine (JVM) from recognizing it as the application's starting point.
Question 2: Which of the following are keywords used in Java?
- boolean (Correct answer)
- Integer
- super (Correct answer)
- array
Correct answer: boolean
Java keywords are reserved words with predefined meanings that cannot be used as identifiers. `boolean` is a primitive data type keyword used to declare variables that can hold `true` or `false` values. `super` is a keyword used to refer to the immediate parent class object or to invoke a superclass's constructor or method. `Integer` is a class in the `java.lang` package, and `array` is a general concept, not a Java keyword.
Question 3: What identifiers are valid?
- r2d2
- bBb$
- _xpoints
- all of the above (Correct answer)
Correct answer: all of the above
In Java, identifiers (names for variables, methods, classes, etc.) must adhere to specific rules. They must start with a letter, an underscore (`_`), or a dollar sign (`$`). Subsequent characters can include letters, digits, underscores, or dollar signs. All the given options—`r2d2`, `bBb$`, and `_xpoints`—follow these rules, making them valid Java identifiers.
Question 4: The String variable s has "Hello Java" assigned to it in which of the following statements?
- String s[] = "Hello Java";
- String s = new String("Hello Java"); (Correct answer)
- String s = "Hello Java"; (Correct answer)
- new String s = "Hello Java";
Correct answer: String s = new String("Hello Java");
Both `String s = new String("Hello Java");` and `String s = "Hello Java";` are valid ways to assign the literal "Hello Java" to a String variable `s`. The first explicitly creates a new String object using the constructor, while the second uses a String literal, which is a more common and often more efficient approach as Java can reuse String literals from the String pool. Options A and D contain incorrect syntax for String declaration and assignment.
Question 5: Which of the following is true:
- >>> performs a signed shift while >> performs an unsigned shift.
- <<< performs a signed shift while << performs an unsigned shift.
- >> performs signed shift while >>> performs an unsigned shift. (Correct answer)
- << performs a signed shift while <<< performs an insigned shift.
Correct answer: >> performs signed shift while >>> performs an unsigned shift.
In Java, the `>>` operator performs a signed right shift, meaning it shifts bits to the right and fills the leftmost bits with the sign bit (0 for positive, 1 for negative), preserving the number's sign. Conversely, the `>>>` operator performs an unsigned right shift, which shifts bits to the right and always fills the leftmost bits with zeros, regardless of the original sign. This distinction is crucial for bitwise operations, especially with negative numbers.
Question 6: Which declarations would be acceptable if we wanted to represent the snowfall in Gnome, Alaska, for each day of March as an array of 31 floating point numbers?
- double snow[31] = new array[31];
- double[] snow = new double[31]; (Correct answer)
- double snow[] = new double[31]; (Correct answer)
- double snow[31] = new array;
Correct answer: double[] snow = new double[31];
Both `double[] snow = new double[31];` and `double snow[] = new double[31];` are correct and acceptable ways to declare and initialize an array of 31 floating-point numbers (doubles) in Java. The square brackets `[]` indicating an array can be placed either after the data type (`double[]`) or after the variable name (`snow[]`). The other options use incorrect syntax for array initialization, such as `new array[31]` or `new array`.
Question 7: Which of the following statements about a two-dimensional array of numbers is legal?
- int a = new int[5,5];
- int[][]a = new[5]int[5];
- int[5][5]a = new int[][];
- int[]a[] = new int[5][5]; (Correct answer)
Correct answer: int[]a[] = new int[5][5];
In Java, a two-dimensional array is essentially an array of arrays. The declaration `int[]a[]` correctly specifies `a` as an array where each element is itself an array of integers. The initialization `new int[5][5]` then correctly allocates memory for a 5x5 matrix of integers. Other options present incorrect syntax for declaring or initializing two-dimensional arrays in Java.
Which of the following accurately describes the main() method of an application?