Declaring Variables in Java Flashcards
What does the keyword int
specify when declaring a variable in Java?
a) The name of the variable
b) The value of the variable
c) The data type of the variable
d) The length of the variable
c) The data type of the variable
Why does the program throw an error if the variable number
is declared but not initialized?
public class MyFirstProgram { public static void main(String[] args) { int number; System.out.println(number); } }
a) Java doesn’t allow declaring variables without assigning them a value
b) The variable number
is not being referenced
c) The variable number
does not have a value to print
d) The semicolon is missing at the end of the line
c) The variable number does not have a value to print
What is the correct way to initialize a variable number
with the value 20?
a) int number == 20;
b) int number = 20;
c) number = int 20;
d) number int = 20;
b) int number = 20;
What is the primary benefit of using variables in a program?
a) They prevent syntax errors
b) They allow you to store and reference values dynamically
c) They make the program easier to write
d) They automatically optimize the performance
b) They allow you to store and reference values dynamically
What is the proper naming convention for variables in Java?
a) PascalCase
b) camelCase
c) Uppercase letters only
d) Lowercase letters only
b) camelCase
Why did IntelliJ mark the variable varint
with a green underline?
a) It was a syntax error
b) The variable varint had not been initialized
c) It suggested a change to match camelCase convention
d) It detected a naming conflict with Java keywords
c) It suggested a change to match camelCase convention
What is the difference between varint
and varInt
in Java?
a) There is no difference; variable names are not case-sensitive in Java
b) varint
and varInt
refer to different variables since Java is case-sensitive
c) varInt
is a reserved keyword in Java
d) Both are incorrect variable names
b) varint
and varInt
refer to different variables since Java is case-sensitive
What happens if you declare a variable but do not initialize it in Java?
a) The program will throw an error at runtime
b) The variable is automatically assigned a default value
c) The program will run but may produce unpredictable results
d) The variable cannot be used until it is initialized
d) The variable cannot be used until it is initialized
What is a key characteristic of Java that makes it different from languages like Python regarding variable declarations?
a) Variables can be used without being declared
b) Java is a loosely-typed language
c) Variables must be declared with a specific type before assignment
d) Java allows for dynamic typing
c) Variables must be declared with a specific type before assignment
What happens if you try to assign a new value to a variable declared as final?
a) The program will allow the change
b) The compiler will throw an error
c) The old value will be replaced
d) The program will run but issue a warning
b) The compiler will throw an error
In the example with varInt
, why is only the value 50 printed to the console?
public class MyFirstProgram { public static void main(String[] args) { int varInt = 100; varInt = 50; System.out.println(varInt); } }
a) The value of 50 was assigned last, and a variable can hold only one value at a time
b) The program encountered an error and skipped the earlier value
c) The variable varInt
cannot hold values larger than 50
d) Java does not allow variables to be reassigned
a) The value of 50 was assigned last
What is the purpose of using the final
keyword when declaring a variable in Java?
a) To make the variable accessible only in the current class
b) To prevent the variable from being reassigned
c) To make the variable private
d) To indicate that the variable is a global constant
b) To prevent the variable from being reassigned