multiple-choice questions (MCQs) on variables in Java:
What is a variable in Java?
a) A constant value
b) A reserved word
c) A container to store data
d) A Java keyword
Which keyword is used to declare a variable in Java?
a) var
b) int
c) variable
d) declare
What is the correct way to declare a constant variable in Java?
a) final int x = 10;
b) const int x = 10;
c) static int x = 10;
d) final x = 10;
Which of the following is NOT a valid variable name in Java?
a) myVar
b) 1var
c) _var
d) $var
What is the default value of an integer variable in Java if it's not initialized?
a) 0
b) 1
c) -1
d) It depends on the compiler
Which data type is used to store a single character in Java?
a) char
b) character
c) chr
d) single
What is the scope of a local variable in Java?
a) It's accessible throughout the program.
b) It's accessible only within the method or block where it's declared.
c) It's accessible within the class.
d) It's accessible within any class in the same package.
Which statement is used to assign a value to a variable in Java?
a) set
b) assign
c) =
d) ==
What happens if you try to assign a value of a larger data type to a variable of a smaller data type?
a) It throws an error.
b) It automatically converts the value.
c) It truncates the value.
d) It depends on the variable declaration.
Which of the following is a valid way to concatenate two strings in Java?
a) str1 + str2
b) str1 . str2
c) str1 & str2
d) str1 : str2
Answers:
c) A container to store data
b) int
a) final int x = 10;
b) 1var
a) 0
a) char
b) It's accessible only within the method or block where it's declared.
c) =
c) It truncates the value.
a) str1 + str2