Variables and formatting Flashcards

1
Q

What is a variable in Java compared to in everyday life?

a) A storage container
b) A bookmark pointing to data in RAM
c) A function returning a value
d) A type of loop in the program

A

b) A bookmark pointing to data in RAM

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

What is the scope of a variable used for in Java?

a) To determine the data type of the variable
b) To define which methods can access the variable
c) To set a value for the variable
d) To handle memory allocation

A

b) To define which methods can access the variable

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

What is the primary purpose of a String variable in Java?

a) To store numerical values
b) To store ASCII and UTF-based text
c) To store boolean values
d) To store floating-point numbers

A

b) To store ASCII and UTF-based text

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

What type of Java variable directly stores its literal value in memory?

a) Reference type
b) Primitive type
c) Static type
d) Dynamic type

A

b) Primitive type

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

What is a Java reference type primarily used for?

a) To store literal values
b) To act as a bookmark pointing to another memory location
c) To handle boolean logic
d) To store primitive values

A

b) To act as a bookmark pointing to another memory location

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

Which primitive type in Java stores a single ASCII/UTF character?

a) Byte
b) Int
c) Char
d) Float

A

c) Char

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

What primitive type in Java is used for 64-bit floating point numbers?

a) Double
b) Float
c) Int
d) Long

A

a) Double

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

Which Java primitive type is best for storing true/false values?

a) Boolean
b) Byte
c) Int
d) Char

A

a) Boolean

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

What is the main benefit of using wrapper classes in Java?

a) They convert primitive data to object data
b) They decrease memory usage
c) They allow faster access to data
d) They provide additional functionality for primitive types

A

d) They provide additional functionality for primitive types

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

In the HelloWorld example, why must the firstName variable be declared as static?

a) Because it is a primitive type
b) Because the class is static
c) Because it is a local variable
d) Because it is a reference type

A

b) Because the class is static

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

What does the printf method in Java allow you to do?

a) Print a simple string
b) Add formatting rules to customize output
c) Declare variables
d) Format arrays automatically

A

b) Add formatting rules to customize output

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

Which format specifier is used to insert a string variable in a printf statement?

a) %i
b) %s
c) %d
d) %c

A

b) %s

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

Why did the ‘Hello world!’ and ‘Welcome to the world of Java, Aaron!’ outputs appear on the same line in the example?

a) Because they are printed from different methods
b) Because the program uses printf for both statements
c) Because there was no instruction to print them on separate lines
d) Because the System library does not handle strings

A

c) Because there was no instruction to print them on separate lines

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

How do you instruct Java to print on a new line using printf?

a) Use \r
b) Use \n or %n
c) Use a new print statement
d) Use a variable

A

b) Use \n or %n

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

What does the format specifier %d in printf represent?

a) Double precision numbers
b) Integer data
c) String data
d) Character data

A

b) Integer data

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

What is the purpose of the System.lineSeparator() method in Java?

a) To automatically handle line endings across different operating systems
b) To add a new line to the output
c) To format text for specific operating systems
d) To replace the println method

A

a) To automatically handle line endings across different operating systems

17
Q

Why might Windows users need to use \r\n instead of just \n for new lines?

a) Because of differences in character encoding
b) Because \n does not work on Windows
c) Because Windows handles line endings differently than MacOS/Linux
d) Because \r\n is the default for all operating systems

A

c) Because Windows handles line endings differently than MacOS/Linux

18
Q

What does println do differently from printf in Java?

a) It adds a new line automatically
b) It does not accept variables
c) It formats output for the console
d) It requires more memory

A

a) It adds a new line automatically

19
Q

Which Java primitive type stores a 32-bit integer?

a) Float
b) Int
c) Long
d) Byte

A

b) Int

20
Q

What does the %s format specifier do in Java’s printf method?

a) Inserts a string variable into the output
b) Inserts an integer variable into the output
c) Adds a special character
d) Prints a floating-point number

A

a) Inserts a string variable into the output