Chapter 2 : Operators & Statements Flashcards Preview

Java 8 > Chapter 2 : Operators & Statements > Flashcards

Flashcards in Chapter 2 : Operators & Statements Deck (28)
Loading flashcards...
1

For the following code:

int x = 1;
long y = 33;

What is the data type of x * y ?
a) int
b) long
c) double
d) does not compile

b) long

If two values have different data types, Java promotes smaller data type to larger data type of the two

2

For the following code

double x = 39.21;
float y = 2.1;

What is the data type of x + y ?
a) double
b) float
c) exception thrown
d) does not compile

d) does not compile

Literal floating-point numbers default to double.

y = 2.1f would compile and the result of x + y would be a) due to the rule : "If two values have different data types, Java promotes smaller data type to larger data type of the two"

3

For the following code

short x = 10;
short y = 3;

What is the data type of x / y and result?

a) short = 3
b) int = 3
c) float = 3.333333...
d) double = 3.333333...
e) does not compile

b) int = 3

Smaller data types are always promoted to int, even when neither is int

4

For the following code

short x = 10;
float y = 3;

What is the data type of x / y and result?

a) short = 3
b) int = 3
c) float = 3.333333...
d) double = 3.333333...
e) does not compile

c) float = 3.333333...

If one values is integral and the other floating point then Java will promote integral to floating-point data type

float y = 3 compiles just fine even though there is no "f" after the literal, since 3 is an integral. If the number where 3.1 then we would not compile. 3.1f would compile however.

5

Which of the following compile, choose all that apply

a) int a = !5;
b) float b = -3;
c) float c = -0b010;
d) boolean d = -true;
e) boolean e = !false;
f) boolean f = !0;

b) float b = -3;

-3 is an integral so this is fine. If the number was -3.1 then it would be considered double and it would not compile. However -3.1f would be fine.

c) float c = -0b010;

A binary integral. Same a b) essentially

e) boolean e = !false;

"!" operator can be used against booleans only. "-" can only be used with numbers only

6

What will the following statement print?

int x = 5;
int y = x++ + x++ + x++;
System.out.println(y);

a) 15
b) 21
c) 18
d) does not compile

c) 18

Post-increment operator uses the current value of the variable and then immediately increments it (in the same statement). Therefore, the question reads as :

int y = 5 + 6 + 7

7

What will the following statement print?

int x = 5
int y = x++ + ++x + x++;
System.out.println(y);

a) 15
b) 19
c) 21
d) 20

b) 19

The question reads as : int y = 5 + 7 + 7

8

What will the following statements print?

int x = 3;
int y = ++x * 5 / x-- + --x;
System.out.print(x + " ");
System.out.println(y);

a) 4 8
b) 2 8
c) 2 7
d) 9 2

c) 2 7

int y = ++x * 5 / x-- + --x;
int y = 4 * 5 / x-- + --x;
int y = 4 * 5 / 4 + --x;         // use 4 but decrement to x to 3
int y = 4 * 5 / 4 + 2;           // x is now 2
int y = 20 / 4 + 2;
int y = 5 + 2;  
int y = 7;

9

What will the following code print?

byte x = 2;
byte y = 4;
byte z = 2 * 4;
System.out.println(z);

a) 8
b) 1
c) exception thrown
d) does not compile

a) 8

Java relaxes its assignment conversion rule for literals. It is ok to assign a literal int value (2 * 4) to a narrower primitive type (byte, short, or char) as long as the literal value falls within the legal range of the primitive type.

 

http://www.java2s.com/Tutorial/SCJP/0080__Type-Casting/Javarelaxesitsassignmentconversionrule.htm

10

What will the following code print?

byte x = 2;
byte y = 4;
byte z = x * y;
System.out.println(z);

a) 8
b) 1
c) exception thrown
d) does not compile

d) does not compile

x * y results in an int but it cannot be placed in a byte variable z. Java does not relax is assignment conversion rule for variables, only for literal integrals.

11

What will the following code print?

short x = 2;
short y = 4;
short z = (byte)(x * y);
System.out.println(z);

a) 8
b) 1
c) exception thrown
d) does not compile

a) 8

It compiles. There are a few things happening here.

  • x * y are turned into int because of the * operator
  • casting from int to byte solves potential compilation error
  • stuffing a byte into a short is ok (smaller to larger is fine)

12

What will the following code print?

short x = 2;
short y = 4;
x *= y;
System.out.println(x);

a) 8
b) 1
c) exception thrown
d) does not compile

a) 8

It compiles. x = x * y does not compile but... x *= y compiles because the compound assignment operator automatically casts to the correct type

13

What will the following code print?

short x = 2;
short y = 4;
short z = x = x = y;
System.out.println(z);

a) 4
b) 2
c) exception thrown
d) does not compile

a) 4

The result of an assignment operation returns the value being assigned. Therefore:

short z = x = x = y;
short z = x = x = 4;
short z = x = 4;
short z = 4;

14

What are the return values for variables a and b if:

boolean x = true;
boolean y = false;
boolean a = x ^ y;
boolean b = x ^ x;

a) a is true, b is true
b) a is true, b is false
c) a is false, b is true
d) a is false, b is false

b) a is true, b is false

The exclusive OR is true only when at most one side of the expression is true. If both sides are true (or false) then it returns false.

______|_TRUE __|_ FALSE
TRUE__|_FALSE_|_TRUE
FALSE_|_TRUE__|_FALSE

15

For the following statement, choose all that apply

int z = 0b101L == 5.00f ? 4 : 3;
System.out.print(z);

a) 4 is printed
b) 3 is printed
c) Does not compile
d) Exception is thrown

a) 4 is printed 

0b101L is a long 5.00 is a float.

The equality operator automatically promotes the smaller numeric primitive to the larger primitive's type

16

What will the following print?

4   int x = 10;
5   int y = x > 10 ? 2 * x : 3 * x;
6   System.out.println( (y > 30 ? y++ : "Orca") + y);

a) Orca31
b) Orca30
c) 2121
d) does not compile

b) Orca30

Ternary operator does not require both statements right of the "?" to be of equal types. In this example we have an int (y++) and a String ("Orca")

17

In the following code segment, select all choices that apply:

2    final char Tuesday = 't';
3    int id = 0;
4    char dayOfWeek = 'm';
5    switch (dayOfWeek) {
6       case 'm':
7            id = 1; break;
8       default:
9            id = 0; break;
10      case Tuesday:
11           id = 2; break;
12      case "Wednesday":
13          id = 3; break;
14   }

a) Line 6 does not compile
b) Line 8 does not compile
c) Line 10 does not compile
d) Line 12 does not compile

d) Line 12 does not compile

b) Line 8 compiles because default can be anywhere in the switch statement. Doesn't have to be at the end
c) Line 10 compiles because variables are allowed only when they are marked final. If Tuesday was not final then this would not compile

18

Choose all that apply

6     final byte r = 3;
7     final short m = 0;
8     final long s = 5;
9     int id = 0;
10    long dayOfWeek = 6;
11     switch (dayOfWeek) {
12        case m:
13            id = 1; break;
14        case r:
15            id = 4; break;
16        case s:
17            id = 5; break;
18    }

a) Line 11 does not compile
b) Line 12 does not compile
c) Line 14 does not compile
d) Line 16 does not compile

a) Line 11 does not compile

Switch statements do not support long (or boolean)

19

Choose all that apply:

2     final char Tuesday = 't';
3     int id = 0;
4     char dayOfWeek = 's';
5     switch (dayOfWeek) {
6         case 'm':
7              id = 1;
8         default:
9              id = 0;
10       case Tuesday:
11             id = 2;
12       case 'w':
13            id = 3;
14    }

a) Line 8 does not compile
b) Line 10 does not compile
c) id = 3;
d) id = 0;

c) id = 3;

Notice that break statements are missing. The default path is chosen and then it falls thru to Tuesday and 'w' case statements.

20

What will the following loop output? 

for ( ; ; ) { System.out.println("Hello World!"); } 

a) Does not compile 
b) Prints "Hello World!" once 
c) Prints "Hello World!" infinitely 
d) Exception thrown

c) Prints "Hello World!" infinitely 

21

What will the following loop output?

int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {

System.out.print(y + " ");

}

a) 0 1 2 3 4
b) 0 1 2 3 4 5 6 7 8 9
c) Does not compile
d) Exception thrown

a) 0 1 2 3 4

It is OK to use a variable not declared inside the for loop (such as x) or to declare variables which are not used (such as z).

22

What will the following loop output?

int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {

System.out.print(y + " ");

}

a) 0 1 2 3 4
b) 4
c) Does not compile
d) Exception thrown

c) Does not compile

x is re-declared inside the for-loop

23

What will the following loop output?

int x = 0;

for (long y = 0, int z = 4; x < 5 && y < 10; x++, y++) {

System.out.print(y + " ");

}

a) 0 1 2 3 4
b) 0 1 2 3 4 5 6 7 8 9
c) Does not compile
d) Exception thrown

c) Does not compile

The type can only appear once at the beginning in the initialization section of the for-loop block (same as a normal one-line multi-variable declaration)

  • long y = 0, int z = 4;       // DOES NOT COMPILE
  • long y = 0, long z = 4;    // DOES NOT COMPILE
  • y = 0, long z = 4;            // DOES NOT COMPILE 

(Page 82)

24

What will the following loop output?

int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
     System.out.print(y + " ");
}
System.out.print(y + " ");

a) 0 1 2 3 4 5
b) 0 1 2 3 4 5 6 7 8 9 10
c) Does not compile
d) Exception thrown

c) Does not compile

y was declared in the for-loop and goes out-of-scope by the second print statement

25

What will the following loop output?

java.util.List values = new java.util.ArrayList();
values.add("Lisa");
values.add("Bart");
values.add("Maggie");
for (String value : values) {
       System.out.print(value + " ");
}

a) Maggie Bart Lisa
b) Lisa Bart Maggie
c) Does not compile
d) Exception thrown

b) Lisa Bart Maggie

26

Which of the following allow labels? Choose all that apply.

a) if
b) while
c) do while
d) for
e) switch

a), b), c), d), e)

27

Which of the following allows unlabeled break? Choose all that apply

a) if
b) while
c) do while
d) for
e) switch

b), c), d), e)

28

Which of the following allows continue statement ? 

a) if 
b) while 
c) do while 
d) for 
e) switch

b), c), d)