chapter 2 sample exam questions Flashcards
Working with Java data types
Given: int myChar = 97; int yourChar = 98; System.out.print((char)myChar + (char)yourChar); int age = 20; System.out.print(" "); System.out.print((float)age); What is the output? a 195 20.0 b 195 20 c ab 20.0 d ab 20 e Compilation error f Runtime exception
Answer: a
Explanation: When a char primitive data type is used as an operand to arithmetic
operators, its corresponding ASCII value is used in the arithmetic operation. Though
(char)myChar explicitly casts int variable myChar to char type, its value 97 is used in
the arithmetic operation. When literal value 20 is explicitly cast to a float type, it out-puts its value as a decimal number, that is, 20.0.
Which of the options are correct for the following code?
public class Prim { // line 1
public static void main(String[] args) { // line 2
char a = ‘a’; // line 3
char b = -10; // line 4
char c = ‘1’; // line 5
141 Answers to sample exam questions
integer d = 1000; // line 6
System.out.println(++a + b++ * c - d); // line 7
} // line 8
} // line 9
a Code at line 4 fails to compile.
b Code at line 5 fails to compile.
c Code at line 6 fails to compile.
d Code at line 7 fails to compile.
Answer: a, c, d Explanation: Option (a) is correct. The code at line 4 fails to compile because you can’t assign a negative value to a primitive char data type without casting. Option (c) is correct. There is no primitive data type with the name “integer.” The valid data types are int and Integer (a wrapper class with I in uppercase). Option (d) is correct. The variable d remains undefined on line 7 because its dec-laration fails to compile on line 6. So the arithmetic expression (++a + b++ * c - d) that uses variable d fails to compile. There are no issues with using the variable c of the char data type in an arithmetic expression. The char data types are internally stored as unsigned integer values and can be used in arithmetic expressions.
What is the output of the following code? public class Foo { public static void main(String[] args) { int a = 10; long b = 20; short c = 30; System.out.println(++a + b++ * c); } } a 611 b 641 c 930 d 960
Answer: a
Explanation: The prefix increment operator (++) used with the variable a will incre-ment its value before it’s used in the expression ++a + b++ * c. The postfix increment
operator (++) used with the variable b will increment its value after its initial value is
used in the expression ++a + b++ * c.
Therefore, the expression ++a + b++ * c evaluates with the following values:
11 + 20 * 30
Because the multiplication operator has a higher precedence than the addition oper-ator, the values 20 and 30 are multiplied before the result is added to the value 11.
The example expression evaluates as follows:
(++a + b++ * c)
= 11 + 20 * 30
= 11 + 600
= 611
EXAM TIP Although questions 2-2 and 2-3 seemed to test you on your under-standing of operators, they actually tested you on different topics. Question 2-2
tested you on the name of the primitive data types. Beware! The real exam
has many such questions. A question that may seem to test you on threads
may actually be testing you on the use of a do-while loop.
Given: Boolean buy = new Boolean(true); Boolean sell = new Boolean(true); System.out.print(buy == sell); boolean buyPrim = buy.booleanValue(); System.out.print(!buyPrim); System.out.print(buy && sell); What is the output? a falsefalsefalse b truefalsetrue c falsetruetrue d falsefalsetrue e Compilation error f Runtime exception
Answer: d
Explanation: The Boolean instances buy and sell are created using constructors.
Constructors don’t refer to existing instances in cache; they create new instances.
Because the comparison operator == compares object references and not the primi-tive value stored by a wrapper instance, buy == sell returns false.
The method booleanValue() can be used to get the primitive boolean value
stored by a Boolean wrapper instance. So buy.booleanValue() returns false. Because
wrapper instances can be used with arithmetic and logical operators, buy && sell com-piles, returning true.
Which of the following options contain correct code to declare and initialize variables to store whole numbers? a bit a = 0; b integer a2 = 7; c long a3 = 0x10C; d short a4 = 0512; e double a5 = 10; f byte a7 = -0; g long a8 = 123456789;
Answer: c, d, f, g
Explanation: Options (a) and (b) are incorrect. There are no primitive data types in
Java with the names bit and integer. The correct names are byte and int.
Option (c) is correct. It assigns a hexadecimal literal value to the variable a3.
Option (d) is correct. It assigns an octal literal value to the variable a4.
Option (e) is incorrect. It defines a variable of type double, which is used to store
decimal numbers, not integers.
Option (f) is correct. -0 is a valid literal value.
Option (g) is correct. 123456789 is a valid integer literal value that can be assigned
to a variable of type long.
Select the options that, when inserted at // INSERT CODE HERE, will make the fol-lowing code output a value of 11: public class IncrementNum { public static void main(String[] args) { int ctr = 50; // INSERT CODE HERE System.out.println(ctr % 20); } } a ctr += 1; b ctr =+ 1; c ++ctr; d ctr = 1;
Answer: a, c
Explanation: To output a value of 11, the value of the variable ctr should be 51
because 51%20 is 11. Operator % outputs the remainder from a division operation. The
current value of the variable ctr is 50. It can be incremented by 1 using the correct
assignment or increment operator.
Option (b) is incorrect. Java does not define a =+ operator. The correct operator
is +=.
Option (d) is incorrect because it’s assigning a value of 1 to the variable result,
not incrementing it by 1.
What is the output of the following code? int a = 10; int b = 20; int c = (a * (b + 2)) - 10-4 * ((2*2) - 6; System.out.println(c); a 218 b 232 c 246 d Compilation error
Answer: d
Explanation: First of all, whenever you answer any question that uses parentheses to
override operator precedence, check whether the number of opening parentheses
matches the number of closing parentheses. This code won’t compile because the
number of opening parentheses doesn’t match the number of closing parentheses.
Second, you may not have to answer complex expressions in the real exam. When-ever you see overly complex code, look for other possible issues in the code. Complex
code may be used to distract your attention from the real issue.
What is true about the following lines of code? boolean b = false; int i = 90; System.out.println(i >= b); a Code prints true b Code prints false c Code prints 90 >= false d Compilation error
Answer: d
Explanation: The code will fail to compile; hence, it can’t execute. You can’t compare
incomparable types, such as a boolean value with a number.
Examine the following code and select the correct options: public class Prim { // line 1 public static void main(String[] args) { // line2 int num1 = 12; // line 3 float num2 = 17.8f; // line 4 boolean eJavaResult = true; // line 5 boolean returnVal = num1 >= 12 && num2 < 4.567 // line 6 || eJavaResult == true; System.out.println(returnVal); // line 7 } // line 8 } // line 9 a Code prints false b Code prints true c Code will print true if code on line 6 is modified to the following: boolean returnVal = (num1 >= 12 && num2 < 4.567) || eJavaResult == true; d Code will print true if code on line 6 is modified to the following: boolean returnVal = num1 >= 12 && (num2 < 4.567 || eJavaResult == false);
Answer: b, c
Explanation: Option (a) is incorrect because the code prints true.
Option (d) is incorrect because the code prints false.
The code in option (c) uses parentheses to indicate which expression should eval-uate prior to the rest. Here are the steps of execution:
boolean returnVal = (num1 >= 12 && num2 < 4.567) || eJavaResult == true;
returnVal = false || eJavaResult == true;
returnVal = true;
The original code in the question doesn’t use parentheses to group the expressions.
In this case, because the operator && has a higher operator precedence than ||, the
expression ‘num1 >= 12 && num2 < 4.567’ will be the first expression to execute. Here
are the steps of execution:
boolean returnVal = num1 >= 12 && num2 < 4.567 || eJavaResult == true;
returnVal = false || eJavaResult == true;
returnVal = true;
Given:
boolean myBool = false; // line 1
int yourInt = 10; // line 2
float hisFloat = 19.54f; // line 3
System.out.println(hisFloat = yourInt); // line 4
System.out.println(yourInt > 10); // line 5
System.out.println(myBool = false); // line 6
What is the result?
a true
true
false
b 10.0
false
false
c false
false
false
d Compilation error
Answer: b
Explanation: The expression myBool = false uses the assignment operator (=) and
not a comparison operator (==). This expression assigns the boolean literal false to
myBool; it doesn’t compare false with myBool. Watch out for similar (trick) assign-ments in the exam, which may seem to be comparing values.