Module 3: Conditional statements, mathematical functions and operations on characters and strings Flashcards

(184 cards)

1
Q

What is a selection statement?

A

A statement that lets you choose actions with alternative courses based on conditions

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

What is a Boolean expression?

A

An expression that evaluates to a Boolean value: true or false

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

What is the boolean data type used for?

A

To declare a variable with the value either true or false

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

What are the 6 relational operators in Java?

A

< (less than), <= (less than or equal), > (greater than), >= (greater than or equal), == (equal to), != (not equal to)

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

What is the equality testing operator?

A

== (two equal signs, not one)

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

What is the difference between = and ==?

A

= is assignment operator, == is equality testing operator

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

What are Boolean literals?

A

true and false (not keywords, but reserved words)

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

What is the syntax for a one-way if statement?

A

if (boolean-expression) { statement(s); }

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

What shape represents a Boolean condition in a flowchart?

A

Diamond box

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

What shape represents statements in a flowchart?

A

Rectangle box

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

Can you omit braces in an if statement?

A

Yes, if there’s only one statement, but it’s error-prone

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

What is the syntax for a two-way if-else statement?

A

if (boolean-expression) { statements-for-true; } else { statements-for-false; }

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

What is a nested if statement?

A

An if statement inside another if statement

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

What is a multi-way if-else statement?

A

A series of if-else statements for multiple alternatives, preferred style for readability

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

What are the 4 logical operators in Java?

A

! (not), && (and), || (or), ^ (exclusive or)

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

What does the ! operator do?

A

Negates true to false and false to true

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

When is && (and) true?

A

Only when both operands are true

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

When is || (or) true?

A

When at least one operand is true

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

When is ^ (exclusive or) true?

A

When operands have different Boolean values (p1 ^ p2 same as p1 != p2)

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

What are short-circuit operators?

A

&& and || - they don’t evaluate second operand if first determines the result

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

What is De Morgan’s law?

A

!(condition1 && condition2) = !condition1 || !condition2, and !(condition1 || condition2) = !condition1 && !condition2

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

What data types can be used in switch expressions?

A

char, byte, short, int, String

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

What is fall-through behavior in switch?

A

When break is omitted, execution continues to next case

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

What is the syntax for switch statement?

A

switch (expression) { case value1: statements; break; default: statements; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Is the default case required in switch?
No, it's optional
26
What is a conditional operator?
?: operator, also called ternary operator
27
What is the syntax for conditional operator?
boolean-expression ? expression1 : expression2
28
How many operands does the ternary operator take?
Three operands
29
What are the common errors with if statements?
Forgetting braces, wrong semicolon, redundant Boolean testing, dangling else, floating-point equality
30
What is dangling else ambiguity?
When else clause matches the most recent unmatched if in same block
31
Why shouldn't you test floating-point equality with ==?
Limited precision causes round-off errors, use Math.abs(x - y) < EPSILON instead
32
What is EPSILON typically set to?
10^-14 for double, 10^-7 for float
33
What does Math.abs() return?
The absolute value of a number
34
What does Math.pow(a, b) return?
a raised to the power of b (a^b)
35
What does Math.sqrt(x) return?
Square root of x
36
What does Math.random() return?
Random double value >= 0.0 and < 1.0
37
How do you generate random integer between 0 and 9?
(int)(Math.random() * 10)
38
How do you generate random integer between 50 and 99?
50 + (int)(Math.random() * 50)
39
What are the trigonometric methods in Math class?
sin, cos, tan, asin, acos, atan, toRadians, toDegrees
40
What units do sin, cos, tan expect?
Radians
41
How do you convert degrees to radians?
Math.toRadians(degrees) or degrees * Math.PI / 180
42
How do you convert radians to degrees?
Math.toDegrees(radians) or radians * 180 / Math.PI
43
What does Math.PI represent?
The value of π (pi)
44
What does Math.E represent?
The base of natural logarithms
45
What are the exponent methods?
exp(x), log(x), log10(x), pow(a,b), sqrt(x)
46
What does Math.exp(x) return?
e raised to power x
47
What does Math.log(x) return?
Natural logarithm of x (ln x)
48
What does Math.log10(x) return?
Base 10 logarithm of x
49
What are the rounding methods?
ceil(x), floor(x), rint(x), round(x)
50
What does Math.ceil(x) do?
Rounds UP to nearest integer (as double)
51
What does Math.floor(x) do?
Rounds DOWN to nearest integer (as double)
52
What does Math.rint(x) do?
Rounds to nearest integer, even one if tie (as double)
53
What does Math.round(x) return?
int for float input, long for double input
54
What does Math.max(a, b) return?
The larger of two values
55
What does Math.min(a, b) return?
The smaller of two values
56
Why doesn't Math class need to be imported?
It's in java.lang package, which is automatically imported
57
What is the char data type?
Represents a single character
58
How are char literals enclosed?
In single quotation marks ' '
59
What is the difference between 'A' and \"A\"?
'A' is char literal, \"A\" is string literal
60
What is Unicode?
Encoding scheme to support world's diverse languages
61
How many bits is original Unicode?
16 bits
62
What is the Unicode range?
\\u0000 to \\uFFFF
63
What does ASCII stand for?
American Standard Code for Information Interchange
64
How many bits is ASCII?
8 bits
65
What is the ASCII code for 'A'?
65 (\\u0041)
66
What is the ASCII code for 'a'?
97 (\\u0061)
67
What is the ASCII code for '0'?
48 (\\u0030)
68
What are escape sequences?
Special notation with backslash to represent special characters
69
What is \\t?
Tab character
70
What is \\n?
Newline character
71
What is \\r?
Carriage return
72
What is \\b?
Backspace
73
What is \\\\?
Backslash character
74
"What is \\\"?","Double quote character" "What is the escape character?","Backslash \\" "Can you cast char to numeric types?","Yes
uses Unicode value"
75
Can you cast numeric types to char?
Yes, but only lower 16 bits used for integers
76
What happens with (int)'A'?
Returns 65 (Unicode of A)
77
Can you use increment/decrement on char?
Yes, gets next/previous Unicode character
78
How do you test if character is digit?
Character.isDigit(ch)
79
How do you test if character is letter?
Character.isLetter(ch)
80
How do you test if character is uppercase?
Character.isUpperCase(ch)
81
How do you test if character is lowercase?
Character.isLowerCase(ch)
82
How do you convert char to lowercase?
Character.toLowerCase(ch)
83
How do you convert char to uppercase?
Character.toUpperCase(ch)
84
Where is Character class located?
java.lang package
85
What is a String in Java?
A sequence of characters, reference type
86
How are String literals enclosed?
In double quotation marks \" \"
87
What type of data type is String?
Reference type (not primitive)
88
What is a reference variable?
Variable that references an object
89
What is the difference between instance and static methods?
Instance methods called on objects, static methods called on classes
90
How do you call an instance method?
referenceVariable.methodName(arguments)
91
How do you call a static method?
ClassName.methodName(arguments)
92
What does string.length() return?
Number of characters in the string
93
What does string.charAt(index) return?
Character at specified index (0-based)
94
What happens if you use charAt with invalid index?
StringIndexOutOfBoundsException
95
How do you concatenate strings?
Using + operator or concat() method
96
What does string1.concat(string2) do?
Returns new string combining string1 and string2
97
What does += do with strings?
Appends right string to left string
98
What does string.toLowerCase() return?
New string with all lowercase letters
99
What does string.toUpperCase() return?
New string with all uppercase letters
100
What does string.trim() return?
New string with leading/trailing whitespace removed
101
What are whitespace characters?
Space, \\t, \\f, \\r, \\n
102
How do you read a string with Scanner?
input.next() for single word, input.nextLine() for entire line
103
What is token-based input?
Input methods that read individual elements (next, nextInt, etc.)
104
What is line-based input?
nextLine() method that reads entire line until Enter
105
Should you mix line-based and token-based input?
No, avoid to prevent input errors
106
How do you read a character from console?
Use nextLine() then charAt(0)
107
How do you compare string contents?
Use equals() method, not == operator
108
What does == do with strings?
Checks if variables refer to same object
109
What does string1.equals(string2) return?
true if strings have same content
110
What does string1.compareTo(string2) return?
0 if equal, negative if string1 < string2, positive if string1 > string2
111
What does equalsIgnoreCase() do?
Compares strings ignoring case differences
112
What does startsWith(prefix) return?
true if string starts with specified prefix
113
What does endsWith(suffix) return?
true if string ends with specified suffix
114
What does contains(substring) return?
true if string contains specified substring
115
Can you use >, <, >= with strings?
No, causes syntax error. Use compareTo() instead
116
What does substring(beginIndex) return?
Substring from beginIndex to end of string
117
What does substring(beginIndex, endIndex) return?
Substring from beginIndex to endIndex-1
118
What does indexOf(ch) return?
Index of first occurrence of character, -1 if not found
119
What does lastIndexOf(ch) return?
Index of last occurrence of character, -1 if not found
120
How do you convert string to int?
Integer.parseInt(string)
121
How do you convert string to double?
Double.parseDouble(string)
122
How do you convert number to string?
number + \"\" or String.valueOf(number)
123
Where are Integer and Double classes?
java.lang package (auto-imported)
124
What does printf stand for?
Print formatted
125
What is a format specifier?
Specifies how an item should be formatted, starts with %
126
What is %d format specifier?
Decimal integer
127
What is %f format specifier?
Floating-point number
128
What is %c format specifier?
Character
129
What is %s format specifier?
String
130
What is %b format specifier?
Boolean value
131
What is %e format specifier?
Scientific notation
132
How many digits after decimal does %f show by default?
6 digits
133
What does %4.2f mean?
Width 4, 2 digits after decimal point
134
How do you left-justify in printf?
Use minus sign: %-10s
135
How do you add leading zeros?
Use 0: %08d
136
How do you add thousand separators?
Use comma: %,8d
137
What happens if item is wider than specified width?
Width automatically increased
138
How do you display literal % in printf?
Use %%
139
Must printf items match format specifiers exactly?
Yes, in order, number, and exact type
140
What is wrong with using int for %f?
Type mismatch - %f requires floating-point
141
What does System.currentTimeMillis() return?
Current time in milliseconds since Unix epoch
142
How do you generate random single digit?
(int)(System.currentTimeMillis() % 10)
143
What is lexicographic ordering?
Dictionary/alphabetical ordering based on Unicode values
144
What does 'a' < 'b' evaluate to?
true (97 < 98 in Unicode)
145
What does 'A' < 'a' evaluate to?
true (65 < 97 in Unicode)
146
Can you perform arithmetic on char?
Yes, automatically cast to int using Unicode values
147
What does '2' + '3' equal?
101 (50 + 51 in Unicode, not 5)
148
What is an empty string?
String with no characters: \"\"
149
What does \"\".length() return?
150
What does Character.isLetterOrDigit(ch) test?
Whether character is letter or digit
151
What does Math.rint(2.5) return?
2.0 (rounds to even number)
152
What does Math.rint(3.5) return?
4.0 (rounds to even number)
153
What does Math.round(2.5f) return?
3 (rounds up for .5)
154
What does Math.round(2.5) return?
3 (long return type)
155
What does Math.ceil(-2.1) return?
-2.0 (rounds toward positive infinity)
156
What does Math.floor(-2.1) return?
-3.0 (rounds toward negative infinity)
157
What range does Math.asin return?
-π/2 to π/2 radians
158
What range does Math.acos return?
0 to π radians
159
What range does Math.atan return?
-π/2 to π/2 radians
160
How do you generate random number between a and a+b-1?
a + (int)(Math.random() * b)
161
What does 30 degrees equal in radians?
π/6 or about 0.5236
162
What does 90 degrees equal in radians?
π/2 or about 1.5708
163
What does 180 degrees equal in radians?
π or about 3.14159
164
Is String a primitive type?
No, it's a reference type (class)
165
What does string concatenation do with numbers?
Converts numbers to strings first
166
What does \"1\" + 1 + 1 equal?
\"111\" (string concatenation)
167
What does 1 + 1 + \"1\" equal?
\"21\" (addition then concatenation)
168
What does \"1\" + (1 + 1) equal?
\"12\" (parentheses force addition first)
169
Can you change a String object after creation?
No, Strings are immutable
170
Do string methods modify the original string?
No, they return new string objects
171
What happens in switch without break?
Fall-through: execution continues to next case
172
Can you have duplicate case values in switch?
No, compile error
173
What is the order of evaluation in logical operators?
! (highest), &&, || (lowest)
174
What does true && false || true evaluate to?
true (evaluated left to right: (true && false) || true)
175
What does !true || false && true evaluate to?
false (!true is false, false && true is false, false || false is false)
176
What is the result of 'a' - 'A'?
32 (97 - 65 = 32)
177
How do you check if string is empty?
str.length() == 0 or str.equals(\"\")
178
What does str.indexOf(\"abc\", 5) do?
Finds \"abc\" starting search from index 5
179
What does str.lastIndexOf('o', 5) do?
Finds last 'o' before or at index 5
180
What error occurs with invalid string index?
StringIndexOutOfBoundsException
181
What error occurs with invalid parseInt?
NumberFormatException
182
Can you use printf with different argument types?
Yes, but they must match format specifiers exactly
183
What does incremental development mean?
Write small amount of code, test, then add more
184
Why test all cases in programs?
To ensure program works correctly for all possible inputs