Chapter 2 Operators and Statements Flashcards

1
Q

What happens with byte, short, char data types when they are used with Java binary aritmetic operators ?

A

Smaller data types, namely byte, short, and char, are first promoted to int any time they’re used with a Java binary arithmetic operator, even if neither of the operands is int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What types of operators are available in Java ?

A

Three types of operators are available in Java: unary, binary, and ternary. These types of operators can be applied to one, two, or three operands, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will happen while applying operators to data types, when two values have different data types?

A

If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the data type of the result after applying operators to different data types?

A

After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When casting primitives values is required ?

A

Casting primitives is required any time you are going from a larger numerical data type to a smaller numerical data type, or converting from a floating-point number to an integral value.

int x = (int)1.0;

short y = (short)1921222;

int z = (int)9l;

long t = 192301398193810323L;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will be printed:

long x = 5;

long y = (x=3);

System.out.println(x);

System.out.println(y);

A

Console output:

3
3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the result of:

short x = 10;

short y = 3;

short z = x * y;

A

Does not compile

(promoted to int when applying any arithmetic operator, with the resulting value being of type int)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between &&,|| and &,| operators ?

A

The short-circuit operators (&&, ||) are nearly identical to the logical operators (&, |) except that the right-hand side of the expression may never be evaluated if the final result can be determined by the left-hand side of the expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will be the output:

int y = 1;

int z = 1;

final int x = y<10 ? y++ : z++;

System.out.println(y+”,”+z);

A

Console output:

2,1
As of Java 7, only one of the right-hand expressions of the ternary operator will be evaluated at runtime. In a manner similar to the short-circuit operators, if one of the two righthand expressions in a ternary operator performs a side effect, then it may not be applied at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What data types are supported by switch statements ?

A

int and Integer, byte and Byte, short and Short, char and Character, String, enum values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will be the output of:

for ( ; ; ) { System.out.println(“Hello World”); }

A

The code creates an infine loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is label ?

A

A label is an optional pointer to the head of a statement that allows the application flow to jump to it or break from it. It is a single word that is proceeded by a colon (:)
..OUTER_LOOP: for(int[] mySimpleArray : myComplexArray).. they are commonly expressed in uppercase, with underscores between words, to distinguish them from regular variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly