ch2 Flashcards

1
Q

What is an operator?

A

A special symbol that can be applied to a set of variables, values or literals and that returns a result

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

What are the three types of operators in Java

A

3 types of operator are :
1 Unary
2 Binary
3 Ternary

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

What are arithmetic operators?

A

*
/
%

Includes unary operators ++ and –

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

List the numeric promotion rules.

A

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

If one of the values is the int and the other is the float, Java will promote the int to float

Smaller data types namely byte, short, char are first promoted to int anytime thery are used with a java binary arithmetic operator.

After all promotion has occured 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

What is the data type of x / y?
short x = 10;
short y = 3;

A

int

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

What is the data type of x + y?
double x = 39.21;
float y = 2.1;

A

compile time error
bc
float y should be 2.1f;

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

What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;

A

double

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

Will this compile?
float y=13;

A

Yes

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

What is the data type of x * y?
int x = 1;
long y = 33;

A

long

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

Will this compile?
double a = 2.0f;

A

yes

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

What is a unary Operator?

A

An operator that requires exactly one operand, variable to function.

x -> operand

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

Will this compile?
float a=1.0?

A

no
you gotta put :
float a =1.0f;

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

What is the output?
~~~
boolean x = false;
System.out.println(x);
x = !x;
System.out.println(x);
~~~

A

x=false;
x=true;

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

Remember that you cannot do this

boolean hello = true;
+hello;

boolean y = -true

int x =!5;

A

You cannot apply a negation operator to a Boolean expression nor can you apply a logical complement to an operator

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

What is the output
int x = !5;
boolean y = -true;
boolean z = !0;

A

does not compile

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

What is the output?
~~~
int counter = 0;
System.out.println(counter);
System.out.println(++counter);
System.out.println(counter);
System.out.println(counter–);
System.out.println(counter);
~~~

A
System.out.println(counter); // Outputs 0
System.out.println(++counter); // Outputs 1
System.out.println(counter); // Outputs 1
System.out.println(counter--); // Outputs 1
System.out.println(counter); // Outputs 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is an assignment operator?

A

An assignment operator is a binary operator that modifies or assigns the variable on the left hand side of the operator with the result on the right hand side of the equation.

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

What is the output?
int x = 1.0;
short y = 1921222;
int z = 9f;
long t = 192301398193810323;

A

int x = 1.0; // DOES NOT COMPILE
short y = 1921222; // DOES NOT COMPILE
int z = 9f; // DOES NOT COMPILE
long t = 192301398193810323; // DOES NOT COMPILE

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

What’s the output?
short x = 10;
short y = 3;
short z = x * y;

A

z is int. Does not compile
you can make it work by casting it to short
~~~
short x =8;
short y=9;
short sum = (short)(x*y);
System.out.println(sum);
~~~

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

What do you mean by casting primitive values?

A

int x = (int) 1.0;

there is a cost, you can have overflow and underflow.

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

What are compound assignment operators?

A

x +=2;

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

What problem does it solve?

A

Say you have the following -
int x =1;
long y =3;
y *=x;

instead of doing this
y = (int)( x * y);

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

What’s the output of the following?

long x= 5;
long y = (x=3);

What are the values of x and y?

A

3

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

What are relational operators?

A

They compare two expressions and return a boolean value.
<, >, <=. >=, instanceof

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

What is a short-circuit operator?

A

The && and || operators “short-circuit”, meaning they don’t evaluate the right-hand side if it isn’t necessary.

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

What are logical operators?

A

The & and | operators, when used as logical operators, always evaluate both sides.

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

When is equality operator used?

A

Comparing two numeric primitive types.
Comparing two boolean values
Comparing two objects including null and String values

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

What is the output?
~~~
File x = new File(“myFile.txt”);
File y = new File(“myFile.txt”);
File z = x;
System.out.println(x == y);
System.out.println(x == z);

~~~

A

File x = new File(“myFile.txt”);
File y = new File(“myFile.txt”);
File z = x;
System.out.println(x == y); // Outputs false
System.out.println(x == z); // Outputs true

true bc same reference
false bc diff reference

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

What’s the output?
int y = 10;
int x = (y > 5) ? (2 * y) : (3 * y);

A

20

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

Code practice:
Using ternary operator
if y > 10 print y is bigger. else y is smaller

A

System.out.println((y>=10)? (“y is greater”): (“y is smaller”));

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

What is the output?
~~~
System.out.println((y > 5) ? 21 : “Zebra”);
int animal = (y < 91) ? 9 : “Horse”;
~~~

A

does not compile bc you cant assign string to int

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

What are the data types supported by switch statement?

A

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

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

What data types are not supported by switch statement?

A

long and boolean

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

A case statument value must be?

A

a literal, enum constant or final constant variable of the same datatype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
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
37
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
38
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
39
Q

What is an array?

A

An array can hold multiple values of the same data type simultaneously

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

Once an array is created, can its size be changed?

A

No it cannot

41
Q

What is the output?
~~~
int dayOfWeek = 5;
switch(dayOfWeek) {
case 0:
System.out.println(“Sunday”);
default:
System.out.println(“Weekday”);
case 6:
System.out.println(“Saturday”);
break;
}
~~~

A

Weekday
Saturday

Always pay attention to break

42
Q

What a break statement?

A

The break statement can also be used to jump out of a loop.

43
Q

What is a 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 (:).

int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}};
OUTER_LOOP: for(int[] mySimpleArray : myComplexArray) {
INNER_LOOP: for(int i=0; i<mySimpleArray.length; i++) {
System.out.print(mySimpleArray[i]+”\t”);
}
System.out.println();
}

not a good practise so dont learn it.

44
Q

Logical operators

A

&, | and ^

45
Q

What is the result for the following?
x=true
y=true
x&y = ?

x=true
y=false
x&y = ?

x=false & y= false?

A

true
false
false

46
Q

What is a continue statement?

A

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

47
Q

INCLUSIVE OR
x=true | y= true?
x=true | y= false?
x=false | y= false?

A

true
true
false

48
Q

EXCLUSIVE OR
x=true ^ y= true?
x=true ^ y= false?
x=false ^ y= false?

A

false
true
false

49
Q

byte range?

A

-128 to 127

50
Q

long i = Long.parseLong(“2l”);
float f = Float.parseFloat(“2.0f”);
double d = Double.parseDouble(“2D”);

A

no. gives number format exception
yes.
Yes

51
Q

Can you do the following?
long var1 = 0_x_4_13;

A

no can only use _ with digits.

52
Q

which one takes less memory?
float or double

A

float

53
Q

which is more precise?
double or float?

A

double

54
Q

Will the following compile?
float a = 2.3F;
double d = 2.4D;
double dd= 2.3d;

A

yes

55
Q

Can you assign a number value to char?

char A= 65;
System.out.println(A);

A

yes
prints “A”

56
Q

Can you assign a number value to char?

char A=- 65;
System.out.println(A);

A

wont compile

57
Q

Can you assign a number value to char?

char A= (char)-65;
System.out.println(A);

A

compiles and returns ?

58
Q

Can you cast an int to a boolean?

A

no

59
Q

int apple = 3;
apple+=3;
System.out.println(apple);

A

6

60
Q

int apple = 3;
apple=+3;
System.out.println(apple);

A

3

61
Q

char A=20;
System.out.println(A+A);

A

40

62
Q

will it compile?
final byte a = 10;
final byte b= 10;
short c = a+b;

A

yes

63
Q

Name three ways you can create object of the wrapper class

A

Assignment (Autobox)
constructor
static methods

64
Q

Write the autobox way of assigning boolean

A

Boolean luck = true;

65
Q

Write Boolean in constructor

A

Boolean a = new Boolean(“True”);
Boolean a = new Boolean(true);

Boolean a = new Boolean(“TRUE”);

66
Q

Make Boolean using static method

A

Boolean a = Boolean.valueOf(“true”);

67
Q

Make the wrapper class for the following
Character
Integer
Double
Short
Byte
Double
Long

A
Byte a = 10;
		Byte b = new Byte("10");
		Byte c = new Byte((byte)10);
		Byte d = Byte.valueOf((byte) 100);
68
Q

Make for short

A
	
Short a = 10;
Short c = new Short("10");
Short b = new Short((short) 10);
Short dd = Short.valueOf("10");
Short cc = Short.valueOf((short)10);
69
Q

Can you do the following?
Character cccc = new Character(“aa”);

How about
Character cccc = new Character(‘a’);

HOw about?
Character ass = Character.valueOf(“b”);

A

no. you are making a string.

yes

No string and characters do not mix

70
Q

How to parse String to Integer?

A

Integer.parseInt(“23”); //int primitive
Integer.valueOf(“23”) //Integer obj

71
Q

What does the following print?

Long var1= Long.valueOf(-12);
Long var2 = Long.valueOf("-12");
System.out.println(var1==var2);
		
Long var3 = Long.valueOf(23);
Long var4 = Long.valueOf(23);
System.out.println(var4==var3);

Long var5 = Long.valueOf(128);
Long var6 = Long.valueOf(128);
System.out.println(var5==var6);
A

true
true
false

-128 to 127

72
Q
		
		Double var1= Double.valueOf(12);
		Double var2 = Double.valueOf(12);
		System.out.println(var1==var2);
		
		Double var3 = Double.valueOf(23);
		Double var4 = Double.valueOf(23);
		System.out.println(var4==var3);

		Double var5 = Double.valueOf(128);
		Double var6 = Double.valueOf(128);
		System.out.println(var5==var6);
A

false
false
false

73
Q
		
		Float var1= Float.valueOf(12f);
		Float var2 = Float.valueOf(12f);
		System.out.println(var1==var2);
		
		Float var3 = Float.valueOf(23);
		Float var4 = Float.valueOf(23);
		System.out.println(var4==var3);

		Float var5 = Float.valueOf(128);
		Float var6 = Float.valueOf(128);
		System.out.println(var5==var6);
A

false
false
false

74
Q

Integer i1 = new Integer(10);
Integer i2 = new Integer(10);
i1==i2

Integer i3 = Integer.valueOf(10);
Integer i4 = Integer.valueOf(10);

i3==i4

Integer i5 =10
Integer i6 = 10

i5==i6

i1.equals(i2)
i2.equals(i3)
i3.equals(i4)

What if i1 > 128

A

false
true
true
———————–
true
true
true
true

equals = all true
false

75
Q

Integer obj1 =100;
Short obj2 = 100;

obj1.equals(obj2);
obj1 == obj2

A

false
DNC

76
Q

Can you do this?

char b = -10;

A

You cannot assign negative value to primitive data type without casting

77
Q

Can you do the following?
public void myMethod(int weight) {
int weight = 10;
}

A

No DNC because same name

78
Q

List the other of operators. Yes all of htem

A

post unary x++, x–
pre unary ++x, –x
unary +, - , !
Multiplication, division, modulus [+ - ]
addition/substraction +, -
shift operators&raquo_space;, «,&raquo_space;>
relational operators <, >, >=, <=, instance of
equal to, not equal to == !=
logical operators &, ^, |
short circuit &&, ||
ternary
assignment operator =, +=, «=

Post present UM ADD SOME REAL Elegant Long short TeA

79
Q

Does switch statement allows to use continue statement?

A

no

80
Q

Does if allow break statement?

A

no

81
Q

Does if statement allow continue statement

A

no

82
Q

Can you do the following?
int i = Integer.parseInt(“45_98”);

A

no because only string is expected.

83
Q

double inclination2 = 1.20173e5;

A

valid

84
Q

Character is signed or unsigned?

A

unsigned Integer

85
Q

What is casting?

A

forceful conversion of one datatype to another datatype.

86
Q

What does all numeric wrapper class extend?

A

java.lang.Number

87
Q

What does Boolean and Character extend?

A

Object

88
Q

What does all wrapper class implement interface?

A

java.io.Serializable
java.lang.Comparable

89
Q

Can you do the following?
Character a = Character.valueOf(“c”);
System.out.println(c);

A

No. You can do for all wrapper classes except Character.

90
Q

Which two wrapper class doesn’t cache object?

A

decimals
Float and Double

91
Q

What is the range of values for cache objects?

A

-128 to 127.

92
Q

What is the range of values for caching object for Character?

A

0 to 127

93
Q

Can you do the following?
Long a1 =10?

A

No you either have to cast it or do
Long a1 = 10L;
Long a1 = (long)10;

94
Q

Which of the following will compile?
Long a1 = 10L;
Long a2 = new Long(10L);
Long a3 = Long.valueOf(“10”);
Long a4 = Long.valueOf((long) 100L);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);

A

Long a1 = 10L; //yes
Long a2 = new Long(10L); //yes
Long a3 = Long.valueOf(“10”); //Yes
Long a4 = Long.valueOf((long) 100L); //Yes
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);

95
Q

Will the following compile?
Long a3 = Long.valueOf(“10L”);

A

No. Number format exception

96
Q

Will the following compile?
Double a1 = 10D;
Double a2 = new Double(10D);
Double a3 = Double.valueOf(“10d”);
Double a4 = Double.valueOf((double) 100D);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);

A

Yes to all

97
Q

Can you do Double a = 10;
Float a1 = 10.0;

A

No it needs to be
Double a = 10D;
Long l = 10L;
Float a1 = 10.0f;

98
Q

Can you do the following?
Float a3 = Float.valueOf(“10.0”);
Float a4= Float.valueOf((float)10.0f);

A

Yes

99
Q

WAP given
int y=4;
ternary operators to find out what the value of y is.

A

System.out.println((y==1)? “1”:(y==2)?”2”:(y==3)?”3”:(y==4)?”4”:”nothing”);