THINK FIRST·CODE LATER

← Java Programming
Chapter 4 · Week 4

Operators, Expressions, and Type Conversion

Answered 0/39 Correct 0
Sign in to save progress across devices
Q1

What is the value of 17 / 5 in Java?

Q2

What is the value of 17 % 5?

Q3

What is the value of -17 / 5?

Q4

What is the value of -17 % 5?

Q5

What does 17.0 / 5 evaluate to?

Q6

What is the value of 2 + 3 * 4 - 6 / 3?

Q7

What is the value of 10 % 3 * 2?

Q8

Which expression correctly computes the average of two int variables a and b as a double?

Q9

What is the output?

int i = 5;
int j = i++;
System.out.print(i + " " + j);
Q10

What is the output?

int i = 5;
int j = ++i;
System.out.print(i + " " + j);
Q11

What is the value of x after int x = 5; x = x++;?

Q12

What is the output?

int a = 3;
System.out.print(a++ + ++a);
Q13

What is the result of compiling and executing the following?

public class Question6 {
    public static void main(String[] args) {
        int meal = 2;
        int tip  = 4;
        int total = meal + (meal > 1 ? ++tip : --tip);
        System.out.println(tip);
    }
}
Q14

In the previous question, what is the value of total?

Q15

Which line does not compile?

byte b = 10;   // L1
b += 5;        // L2
b = b + 5;     // L3
b++;           // L4
Q16

x *= 2 + 3; is equivalent to:

Q17

Which sequence shows the widening primitive conversions in the correct order?

Q18

Why does short c = a + b; fail to compile when a and b are short?

Q19

What is the value of (int) 9.99?

Q20

What is the value of (int) -9.99?

Q21

What does the following print?

System.out.println('a' + 1);
Q22

What does the following print?

System.out.println((char)('a' + 1));
Q23

Which cast is illegal in Java?

Q24

What is printed by System.out.println(1 + 2 + "3" + 4 + 5);?

Q25

What is printed by System.out.println("" + 1 + 2 * 3);?

Q26

What happens at run time when an int is divided by zero?

Q27

What is the value of 5.0 / 0?

Q28

Why is if (0.1 + 0.2 == 0.3) false?

Q29

What does n % 2 == 0 test?

Q30

What is the value of max after int a = 4, b = 9; int max = (a > b) ? a : b;?

Q31

In the conditional expression cond ? x++ : y++, how many of x and y are incremented?

Q32

What is the output?

int total = 0;
for (int i = 1; i <= 3; i++) {
    total += i * i;
}
System.out.print(total);
Q33

Which expression is equivalent to !(a > b) for numeric a and b?

Q34

What is the result of int x = (int)(7 / 2.0);?

Q35

An int variable holding 2147483647 is incremented by 1. What happens?

Q36 Short answer

Explain, with one line of code each, the difference between (double)(sum / count) and (double) sum / count when sum and count are both int.

Q37 Short answer

Why does b += 5 compile for a byte while b = b + 5 does not? Write the statement that the compiler effectively generates.

Q38 Short answer

Trace int x = 2; int y = x++ * --x + x--; step by step and give the final values of x and y.

Q39 Programming

Write a program SecondsConverter that stores a total number of seconds (for example 9265) in an int and prints it in the form 2 h 34 min 25 s, using only integer division and the remainder operator.