Variables and formatting Flashcards
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
b) A bookmark pointing to data in RAM
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
b) To define which methods can access the variable
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
b) To store ASCII and UTF-based text
What type of Java variable directly stores its literal value in memory?
a) Reference type
b) Primitive type
c) Static type
d) Dynamic type
b) Primitive type
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
b) To act as a bookmark pointing to another memory location
Which primitive type in Java stores a single ASCII/UTF character?
a) Byte
b) Int
c) Char
d) Float
c) Char
What primitive type in Java is used for 64-bit floating point numbers?
a) Double
b) Float
c) Int
d) Long
a) Double
Which Java primitive type is best for storing true/false values?
a) Boolean
b) Byte
c) Int
d) Char
a) Boolean
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
d) They provide additional functionality for primitive types
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
b) Because the class is static
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
b) Add formatting rules to customize output
Which format specifier is used to insert a string variable in a printf statement?
a) %i
b) %s
c) %d
d) %c
b) %s
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
c) Because there was no instruction to print them on separate lines
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
b) Use \n or %n
What does the format specifier %d
in printf
represent?
a) Double precision numbers
b) Integer data
c) String data
d) Character data
b) Integer data
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) To automatically handle line endings across different operating systems
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
c) Because Windows handles line endings differently than MacOS/Linux
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) It adds a new line automatically
Which Java primitive type stores a 32-bit integer?
a) Float
b) Int
c) Long
d) Byte
b) Int
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) Inserts a string variable into the output