2.9. Answers to sample exam questions

Q2-1.

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?

  1. 195 20.0
  2. 195 20
  3. ab 20.0
  4. ab 20
  5. Compilation error
  6. 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 outputs its value as a decimal number, that is, 20.0.

Q2-2.

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

        integer d = 1000;                          // line 6
        System.out.println(++a + b++ * c - d);     // line 7
    }                                              // line 8
}                                                  // line 9

  1. Code at line 4 fails to compile.
  2. Code at line 5 fails to compile.
  3. Code at line 6 fails to compile.
  4. 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 declaration 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.

Q2-3.

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);
    }
}

  1. 611
  2. 641
  3. 930
  4. 960

Answer: a

Explanation: The prefix increment operator (++) used with the variable a will increment 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 operator, 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 understanding 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!

Q2-4.

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?

  1. falsefalsefalse
  2. truefalsetrue
  3. falsetruetrue
  4. falsefalsetrue
  5. Compilation error
  6. 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 primitive 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 compiles, returning true.

Q2-5.

Which of the following options contain correct code to declare and initialize variables to store whole numbers?

  1. bit a = 0;
  2. integer a2 = 7;
  3. long a3 = 0x10C;
  4. short a4 = 0512;
  5. double a5 = 10;
  6. byte a7 = -0;
  7. 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.

Q2-6.

Select the options that, when inserted at // INSERT CODE HERE, will make the following 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);
    }
}

  1. ctr += 1;
  2. ctr =+ 1;
  3. ++ctr;
  4. 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.

Q2-7.

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);

  1. 218
  2. 232
  3. 246
  4. 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. Whenever 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.

Q2-8.

What is true about the following lines of code?

boolean b = false;
int i = 90;
System.out.println(i >= b);

  1. Code prints true
  2. Code prints false
  3. Code prints 90 >= false
  4. 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.

Q2-9.

Examine the following code and select the correct options:

public class Prim {                                          // line 1
    public static void main(String[] args) {                 // line 2
        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

  1. Code prints false
  2. Code prints true
  3. Code will print true if code on line 6 is modified to the following:

    boolean returnVal = (num1 >= 12 && num2 < 4.567) || eJavaResult == true;
  4. 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 evaluate 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;

Q2-10.

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?


  1. true
    true
    false
  2. 10.0 false false

  3. false
    false
    false
  4. 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) assignments in the exam, which may seem to be comparing values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39