Module 2: Elementary Programming Flashcards

(141 cards)

1
Q

What is a variable in Java?

A

A variable represents a value stored in computer memory. It has a name, type, and value.

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

What are the 6 numeric data types in Java?

A

byte, short, int, long, float, double

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

What is the range of byte data type?

A

-128 to 127 (8-bit signed)

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

What is the range of short data type?

A

-32768 to 32767 (16-bit signed)

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

What is the range of int data type?

A

-2147483648 to 2147483647 (32-bit signed)

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

What is the storage size of long data type?

A

64-bit signed

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

What is the storage size of float data type?

A

32-bit IEEE 754

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

What is the storage size of double data type?

A

64-bit IEEE 754

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

How many significant digits does float have?

A

6-9 significant digits

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

How many significant digits does double have?

A

15-17 significant digits

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

What is the syntax for declaring a variable?

A

datatype variableName;

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

What is the syntax for declaring and initializing a variable?

A

datatype variableName = value;

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

Can you declare multiple variables of the same type in one line?

A

Yes: datatype variable1, variable2, variable3;

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

What keyword is used to declare constants in Java?

A

final

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

What is the syntax for declaring a constant?

A

final datatype CONSTANTNAME = value;

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

What are 3 benefits of using constants?

A

1) No repeated typing of same value 2) Easy to change in one location 3) Makes code more readable

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

How do you import the Scanner class?

A

import java.util.Scanner;

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

How do you create a Scanner object?

A

Scanner input = new Scanner(System.in);

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

What method reads a double from keyboard?

A

input.nextDouble()

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

What method reads an int from keyboard?

A

input.nextInt()

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

What method reads a byte from keyboard?

A

input.nextByte()

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

What method reads a short from keyboard?

A

input.nextShort()

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

What method reads a long from keyboard?

A

input.nextLong()

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

What method reads a float from keyboard?

A

input.nextFloat()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is the difference between print() and println()?
println() moves to next line after printing, print() stays on same line
26
What is a prompt in programming?
A message directing the user to enter input
27
What are the 5 basic arithmetic operators?
+ (addition), - (subtraction), * (multiplication), / (division), % (remainder)
28
What happens with integer division 5/2?
Result is 2 (fractional part truncated)
29
What is the result of 7 % 3?
1 (remainder after division)
30
What is the result of 20 % 3?
2
31
What is operator precedence order?
1) Parentheses 2) *, /, % (left to right) 3) +, - (left to right)
32
What is an assignment expression?
An assignment statement that evaluates to the value being assigned
33
What does x += 5 mean?
x = x + 5
34
What does x *= 3 mean?
x = x * 3
35
What does x /= 2 mean?
x = x / 2
36
What does x %= 4 mean?
x = x % 4
37
What does x -= 7 mean?
x = x - 7
38
What are the 4 rules for Java identifiers?
1) Letters, digits, _, $ only 2) Must start with letter, _, or $ 3) Cannot be reserved word 4) Any length
39
Is Java case sensitive?
Yes, area and Area are different identifiers
40
What is camelCase naming convention?
First word lowercase, capitalize first letter of subsequent words (numberOfStudents)
41
What is the naming convention for classes?
Capitalize first letter of each word (ComputeArea)
42
What is the naming convention for constants?
All uppercase with underscores (MAX_VALUE)
43
How do you denote a long integer literal?
Append L or l (example: 2147483648L)
44
How do you denote a float literal?
Append f or F (example: 100.2f)
45
How do you denote a double literal?
Append d or D (example: 100.2d) or no suffix (default)
46
What is scientific notation format in Java?
Use E or e for exponent (1.23456E2 = 123.456)
47
What does + operator do with strings?
String concatenation - combines strings together
48
What happens when you combine a number with a string using +?
Number is converted to string then concatenated
49
How do you break a long string across lines?
Use concatenation: "Part 1" + "Part 2"
50
What is the result of 5.0 / 2?
2.5 (floating-point division)
51
What is the result of 5 / 2?
2 (integer division)
52
What does Math.pow(2, 3) return?
8.0 (2 raised to power 3)
53
What does System.currentTimeMillis() return?
Current time in milliseconds since Unix epoch (Jan 1, 1970 GMT)
54
What is the IPO pattern?
Input-Process-Output programming pattern
55
How do you check if a number is even?
number % 2 == 0
56
How do you check if a number is odd?
number % 2 == 1 (for positive numbers)
57
What runtime error occurs with wrong input type?
InputMismatchException
58
What happens if you use a variable before declaring it?
Compile error
59
What happens if you use a variable before initializing it?
Compile error
60
What is wrong with identifier '2A'?
Cannot start with digit
61
What is wrong with identifier 'class'?
'class' is a reserved word
62
What is JShell?
REPL (Read-Evaluate-Print Loop) tool for executing Java statements
63
How do you launch JShell?
Type 'jshell' in command prompt (JDK 9+ required)
64
What JShell command lists all variables?
/vars
65
What JShell command opens editor?
/edit
66
What JShell command exits JShell?
/exit
67
What is REPL?
Read-Evaluate-Print Loop
68
What is the result of -7 % 3?
-1 (remainder is negative when dividend is negative)
69
What is the result of 20 % -13?
7
70
What is the Unix epoch?
January 1, 1970 00:00:00 GMT
71
What is a unary operator?
Operator with only one operand (like -5)
72
What is a binary operator?
Operator with two operands (like 4 - 5)
73
What are primitive data types also called?
Fundamental types
74
What does IEEE 754 represent?
Standard for representing floating-point numbers
75
Can you assign 1.0 to an int variable?
No, compile error - incompatible types
76
What is tracing a program?
Method of reviewing how a program works by tracking variable values
77
What does 'final' keyword mean?
Variable cannot be changed after initialization
78
What is string concatenation operator?
+ when used with strings
79
What happens in chained assignment i = j = k = 1?
Evaluated right to left: k=1, j=k, i=j
80
What is the default type for integer literals?
int
81
What is the default type for floating-point literals?
double
82
Can constants be declared without initialization?
No, must declare and initialize together
83
What is the $ character used for in identifiers?
Should only be used in mechanically generated code
84
What does 'descriptive names' mean for variables?
Use complete words instead of abbreviations (numberOfStudents vs numStuds)
85
What is the difference between specific and wildcard imports?
Specific: import java.util.Scanner; Wildcard: import java.util.*;
86
Is there performance difference between import types?
No performance difference between specific and wildcard imports
87
What keyword do you use after public class?
class name, then opening brace
88
What is required in every Java program?
public static void main(String[] args) method
89
What does System.out.println() do?
Prints to console and moves to next line
90
What does System.out.print() do?
Prints to console without moving to next line
91
How do you compute area of circle with radius r?
area = radius * radius * PI
92
What value should you use for PI?
3.14159 or Math.PI
93
What is the algorithm for calculating circle area?
1) Read radius 2) Compute area = r*r*PI 3) Display result
94
What does 'no value' mean in variable tracing?
Variable declared but not yet initialized
95
What is concatenating strings with numbers result?
Number converted to string, then concatenated
96
Why use Scanner input = new Scanner(System.in)?
Creates Scanner object to read from standard input (keyboard)
97
What does input.nextDouble() wait for?
User to enter a number and press Enter
98
What should you do when expecting keyboard input?
Always provide a prompt to tell user what to enter
99
How do you handle multiple numeric inputs?
Call multiple next methods or enter numbers separated by spaces
100
What error occurs if you enter '5a' for nextDouble()?
Runtime error (InputMismatchException)
101
What does 'evaluating an expression' mean?
Computing the result following operator precedence rules
102
How are nested parentheses evaluated?
Innermost parentheses first
103
What is the result of 3 + 4 * 4 + 5 * (4 + 3) - 1?
53
104
Why use 5.0/9 instead of 5/9 in temperature conversion?
5/9 gives 0 (integer division), 5.0/9 gives 0.555... (floating-point)
105
How do you convert Fahrenheit to Celsius?
C = (5.0/9) * (F - 32)
106
What does System.nanoTime() return?
Elapsed time in nanoseconds (more precise than currentTimeMillis)
107
What is augmented assignment operator?
Combines assignment with arithmetic operation (+=, -=, *=, /=, %=)
108
Is there space in augmented operators?
No spaces allowed (+= not + =)
109
When is augmented assignment evaluated?
After all other operators in expression
110
What does x /= 4 + 5.5 * 1.5 mean?
x = x / (4 + 5.5 * 1.5)
111
Can augmented operators form expressions?
Yes, x += 2 can be statement or expression
112
How do you use underscores in numeric literals?
Between digits for readability: 232_45_4519
113
Can underscore start or end a numeric literal?
No, 45_ or _45 is incorrect
114
What type is assumed for 123?
int
115
What type is assumed for 123.45?
double
116
How do you make 123 a long?
123L
117
How do you make 123.45 a float?
123.45f
118
What is binary literal prefix?
0b or 0B (example: 0B1111 = 15)
119
What is octal literal prefix?
0 (example: 07777 = 4095)
120
What is hexadecimal literal prefix?
0x or 0X (example: 0XFFFF = 65535)
121
What happens if literal is too large for variable type?
Compile error
122
Why is double preferred over float?
More accurate (15-17 vs 6-9 significant digits)
123
What determines memory allocation for variables?
Data type of the variable
124
Must variables be assigned before use?
Yes, compile error if used before assignment
125
What is variable scope?
Part of program where variable can be referenced
126
What are operands?
Values operated on by an operator
127
What is dividend in remainder operation?
Left operand (in 20 % 3, dividend is 20)
128
What is divisor in remainder operation?
Right operand (in 20 % 3, divisor is 3)
129
When is remainder negative?
Only when dividend is negative
130
How do you find day of week after n days?
(currentDay + n) % 7
131
What day is 0 in week calculation?
Sunday
132
What day is 6 in week calculation?
Saturday
133
How do you extract minutes from total seconds?
totalSeconds / 60
134
How do you find remaining seconds after extracting minutes?
totalSeconds % 60
135
What is integer division used for?
Getting whole number quotient, discarding remainder
136
What is remainder operator used for?
Getting leftover after division
137
How do you check if year is leap year using modulus?
year % 4 == 0 (simplified check)
138
What makes a good algorithm?
Clear, step-by-step instructions to solve problem
139
Should you write algorithm before coding?
Yes, good practice to outline program first
140
What is pseudocode?
Natural language mixed with programming code
141
What does 'code' mean in programming?
Write a program, translate algorithm to programming language