ch2 Flashcards

(99 cards)

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
What is a short-circuit operator?
The && and || operators "short-circuit", meaning they don't evaluate the right-hand side if it isn't necessary.
26
What are logical operators?
The & and | operators, when used as logical operators, always evaluate both sides.
27
When is equality operator used?
Comparing two numeric primitive types. Comparing two boolean values Comparing two objects including null and String values
28
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); ```
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
29
What's the output? int y = 10; int x = (y > 5) ? (2 * y) : (3 * y);
20
30
Code practice: Using ternary operator if y > 10 print y is bigger. else y is smaller
System.out.println((y>=10)? ("y is greater"): ("y is smaller"));
31
What is the output? ``` System.out.println((y > 5) ? 21 : "Zebra"); int animal = (y < 91) ? 9 : "Horse"; ```
does not compile bc you cant assign string to int
32
What are the data types supported by switch statement?
int and Integer byte and Byte char and Character short and Short String enum
33
What data types are not supported by switch statement?
long and boolean
34
A case statument value must be?
a literal, enum constant or final constant variable of the same datatype.
35
When casting primitives values is required ?
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;
36
What will be printed: long x = 5; long y = (x=3); System.out.println(x); System.out.println(y);
Console output: 3 3
37
What is the result of: short x = 10; short y = 3; short z = x * y;
Does not compile (promoted to int when applying any arithmetic operator, with the resulting value being of type int)
38
What is the difference between &&,|| and &,| operators ?
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.
39
What is an array?
An array can hold multiple values of the same data type simultaneously
40
Once an array is created, can its size be changed?
No it cannot
41
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; } ```
Weekday Saturday Always pay attention to break
42
What a break statement?
The break statement can also be used to jump out of a loop.
43
What is a label?
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
44
Logical operators
&, | and ^
45
What is the result for the following? x=true y=true x&y = ? x=true y=false x&y = ? x=false & y= false?
true false false
46
What is a continue statement?
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
47
INCLUSIVE OR x=true | y= true? x=true | y= false? x=false | y= false?
true true false
48
EXCLUSIVE OR x=true ^ y= true? x=true ^ y= false? x=false ^ y= false?
false true false
49
byte range?
-128 to 127
50
long i = Long.parseLong("2l"); float f = Float.parseFloat("2.0f"); double d = Double.parseDouble("2D");
no. gives number format exception yes. Yes
51
Can you do the following? long var1 = 0_x_4_13;
no can only use _ with digits.
52
which one takes less memory? float or double
float
53
which is more precise? double or float?
double
54
Will the following compile? float a = 2.3F; double d = 2.4D; double dd= 2.3d;
yes
55
Can you assign a number value to char? char A= 65; System.out.println(A);
yes prints "A"
56
Can you assign a number value to char? char A=- 65; System.out.println(A);
wont compile
57
Can you assign a number value to char? char A= (char)-65; System.out.println(A);
compiles and returns ?
58
Can you cast an int to a boolean?
no
59
int apple = 3; apple+=3; System.out.println(apple);
6
60
int apple = 3; apple=+3; System.out.println(apple);
3
61
char A=20; System.out.println(A+A);
40
62
will it compile? final byte a = 10; final byte b= 10; short c = a+b;
yes
63
Name three ways you can create object of the wrapper class
Assignment (Autobox) constructor static methods
64
Write the autobox way of assigning boolean
Boolean luck = true;
65
Write Boolean in constructor
Boolean a = new Boolean("True"); Boolean a = new Boolean(true); Boolean a = new Boolean("TRUE");
66
Make Boolean using static method
Boolean a = Boolean.valueOf("true");
67
Make the wrapper class for the following Character Integer Double Short Byte Double Long
``` Byte a = 10; Byte b = new Byte("10"); Byte c = new Byte((byte)10); Byte d = Byte.valueOf((byte) 100); ```
68
Make for short
``` 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
Can you do the following? Character cccc = new Character("aa"); How about Character cccc = new Character('a'); HOw about? Character ass = Character.valueOf("b");
no. you are making a string. yes No string and characters do not mix
70
How to parse String to Integer?
Integer.parseInt("23"); //int primitive Integer.valueOf(“23”) //Integer obj
71
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); ```
true true false -128 to 127
72
``` 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); ```
false false false
73
``` 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); ```
false false false
74
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
false true true ----------------------- true true true true --------------------- equals = all true false
75
Integer obj1 =100; Short obj2 = 100; obj1.equals(obj2); obj1 == obj2
false DNC
76
Can you do this? char b = -10;
You cannot assign negative value to primitive data type without casting
77
Can you do the following? public void myMethod(int weight) { int weight = 10; }
No DNC because same name
78
List the other of operators. Yes all of htem
post unary x++, x-- pre unary ++x, --x unary +, - , ! Multiplication, division, modulus [+ - ] addition/substraction +, - shift operators >>, <<, >>> 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
Does switch statement allows to use continue statement?
no
80
Does if allow break statement?
no
81
Does if statement allow continue statement
no
82
Can you do the following? int i = Integer.parseInt("45_98");
no because only string is expected.
83
double inclination2 = 1.20173e5;
valid
84
Character is signed or unsigned?
unsigned Integer
85
What is casting?
forceful conversion of one datatype to another datatype.
86
What does all numeric wrapper class extend?
java.lang.Number
87
What does Boolean and Character extend?
Object
88
What does all wrapper class implement interface?
java.io.Serializable java.lang.Comparable
89
Can you do the following? Character a = Character.valueOf("c"); System.out.println(c);
No. You can do for all wrapper classes except Character.
90
Which two wrapper class doesn't cache object?
decimals Float and Double
91
What is the range of values for cache objects?
-128 to 127.
92
What is the range of values for caching object for Character?
0 to 127
93
Can you do the following? Long a1 =10?
No you either have to cast it or do Long a1 = 10L; Long a1 = (long)10;
94
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);
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
Will the following compile? Long a3 = Long.valueOf("10L");
No. Number format exception
96
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);
Yes to all
97
Can you do Double a = 10; Float a1 = 10.0;
No it needs to be Double a = 10D; Long l = 10L; Float a1 = 10.0f;
98
Can you do the following? Float a3 = Float.valueOf("10.0"); Float a4= Float.valueOf((float)10.0f);
Yes
99
WAP given int y=4; ternary operators to find out what the value of y is.
System.out.println((y==1)? "1":(y==2)?"2":(y==3)?"3":(y==4)?"4":"nothing");