Syvum Home Page

Home > Quiz Games > Java Programming > Print Preview

Java Programming : Compilation and Execution: Multiple Choice

Formats Info Page Worksheet / Test Paper Quiz Review

Hide all answers   View all answers   Print   Try the Quiz

Answer the following questions about Java syntax and mistakes which can cause compilation errors, as well as the output generated by sample programs.


1. Which of the following expressions will produce errors upon compilation?
(A) boolean a = (boolean) 1;
(B) boolean b = (false && true);
(C) float y = 22.3;
• (A)
• (A), (C) & (D)
• (A), (B) & (D)
• (A) & (C)
Answer: (A) & (C)
Integers cannot be converted to booleans, and floats must be explicitly specified (22.3f, and not 22.3 which is a double).

2. Which lines of the following will produce an error?
1. byte a1 = 2, a2 = 4, a3;
2. short s = 16;
3. a2 = s;
4. a3 = a1 * a2;
(The lines are numbered only for illustration in this question.)
• Line 3 and Line 4
• Line 1 only
• Line 3 only
• Line 4 only
• Line 1 and Line 4
Answer: Line 3 and Line 4
In line 3, a short value is being assigned to a variable of type byte, which is illegal. Also, the "*" operator promotes the bytes to integers, which makes it illegal to assign the return value to a variable of type byte.

3. Examine the following code snippets to identify the legal loop constructs:
(A) for (int i = 22, int j = 0; i + j > 11; i = i-3, j++)

• (A)
• (A) & (D)
• (B)
• (A) & (C)
Answer: (A) & (D)
In option (B), the argument of "while" is an integer, which is illegal. It can only be boolean. In option (C), "int i > 0" is an illegal construct.


4. Determine the output when the value of x is zero:
if(x >= 0)
if(x > 0)
System.out.println("x is positive");
else
System.out.println("x is negative");

• "x is positive"
• "x is positive" and "x is negative"
• "x is negative"
• None of these
Answer: "x is negative"
The "else" statement corresponds to the second "if" in this case. To have it correspond to the first "if", curly braces

5. The control expression in an "if" statement must be:
• an expression with type boolean
• an expression with type integer
• an expression with either the type boolean or integer
• an expression with either the type boolean or integer with value 0 or 1
Answer: an expression with type boolean
In the Java programming language, only expressions of type boolean are allowed as control expressions in an "if" statement.

6. To print the value of a variable "x" of type int, which of the following expressions can be used:
(A) System.out.println("x = " + x);
(B) System.out.println("x = " + String.valueOf(x));
(C) System.out.println("x = " + Integer.toString(x));
(D) System.out.println("x = " + (new Integer(x)).toString());

• (B), (C) and (D)
• (B) and (D)
• (C) and (D)
• (A), (B), (C) and (D)
Answer: (A), (B), (C) and (D)
Other than the functions in the String and Integer class, the + operator can be used directly with one String operand and other one int.

7. Consider the following code:
int i = 1;
switch(c)


• output will be One, followed by Two, and then followed by Three
• output will be One followed by Two
• output will be One
• code is illegal and therefore will not compile
Answer: output will be One, followed by Two, and then followed by Three
The flow of control in switch statements goes subsequently through the case statements unless explicitly broken using the break statement. In which case, the flow transfers to the first statement after the switch block. Without any break statements, all the cases are executed.

8. Consider the following code:
class NewString extends java.lang.String


• Results in error because class body is not defined
• Results in error because java.lang.String isfinal
• Results in error because the class is not declaredpublic
• Results in error because String isabstract
• Compiles successfully
Answer: Results in error because java.lang.String isfinal
java.lang.String is a final class and cannot be extended.

9. Examine the following class definitions to detect errors, if any.
abstract class MyPanel

• 1 Error. Method show() should have a return type
• 1 Error. Method show() is not implemented in MyDisplay
• 1 Error. MyDisplay does not contain any members
• No errors
Answer: 1 Error. Method show() is not implemented in MyDisplay
Class MyDisplay must implement the abstract method show(), or it must also be declared abstract.

10. Are there any errors in the following class definition?
abstract class Class1


• Class header definition is wrong
• Method definition is wrong
• Constructor needs to be defined
• No errors
Answer: Method definition is wrong
The method func1, which is declared as abstract cannot have a body as in the code above.

11. For the following class definition, which is a legal statement to construct an object of type Class1:
class Class1 extends Class2


• (A)
• (C) & (E)
• (A) & (B)
• (C) & (D)
• None of the above
Answer: (C) & (D)
The two constructors defined for Class1 are used in options (C) and (D). All other constructors are invalid.

12. The method int func(int i, int j)
• (B) & (C)
• (C) & (D)
• (A), (B), (C) & (E)
• (A), (B) & (E)
• None of these
Answer: (A), (B) & (E)
Method overloading can be done by specifying different arguments (or types). Only having a different return type causes compilation error (as in C above).

13. Which error does the following code contain: class Class1

• There is no error
• Method func1() must be declared as static
• Class Class1 has not been declared public
• Class1 should be declared as abstract
Answer: Class1 should be declared as abstract
A class containing an abstract method must also be declared abstract.

14. What will be the output of the following program?
class Main1


• XXX
• YYY
• Error. Won't compile
• XXX followed by YYY
Answer: Error. Won't compile
The statement System.out.prinln("YYY"); is not reachable.

15. What will be the output of the following program?
class Main2


• XXX
• YYY
• Error. Won't compile
• XXX followed by YYY
Answer: XXX followed by YYY

16. What is java_g used for?
• Using the jdb tool
• To provide information about deprecated methods
• None of these
• Executing a class with optimization turned off
Answer: Executing a class with optimization turned off

17. With javadoc, which of the following denotes a javadoc comment?
• //#
• /*
• //**
• /**
Answer: /**

18. Which of the following command lines options generates documentation for all classes and methods?
• -protected
• -public
• -private
• -verbose
• -encoding
Answer: -private

19. Which javadoc tag is used to denote a comment for a method parameter?
• @param
• @method
• @parameter
• @argument
• @value
Answer: @param

  Try the Quiz :     Java Programming : Compilation and Execution: Multiple Choice


Contact Info © 1999-2024 Syvum Technologies Inc. Privacy Policy Disclaimer and Copyright
Previous
-
Next
-