Java Variables MCQ - Multiple Choice Questions And Answers
Q. What is the purpose of a variable in Java?
A. To store and manipulate dataB. To define classes
C. To perform mathematical operations
D. To control program flow
Q. What is the data type of the variable "x" in the following code snippet: int x = 5;
A. intB. double
C. boolean
D. String
Q. Which of the following is not a valid variable name in Java?
A. myVariableB. _variable
C. 2variable
D. variable2
Q. What is the default value of an uninitialized int variable in Java?
A. 0B. null
C. false
D. undefined
Q. Which keyword is used to declare a constant variable in Java?
A. constantB. final
C. static
D. const
Q. Which of the following is a valid variable declaration in Java?
A. int x;B. x = 5;
C. var x = 5;
D. All of the above
Q. What is the scope of a local variable in Java?
A. It is accessible throughout the entire programB. It is accessible within the method or block of code where it is declared
C. It is accessible within the class where it is declared
D. It is accessible within the package where it is declared
Q. What is the difference between instance variables and local variables in Java?
A. Instance variables are declared within a method, while local variables are declared within a classB. Instance variables are accessible throughout the entire program, while local variables are only accessible within the method or block of code where they are declared
C. Instance variables are declared within a class, while local variables are declared within a method or block of code
D. There is no difference between instance variables and local variables in Java
Q. What is the purpose of the final keyword in Java?
A. To make a variable constantB. To indicate the end of a program
C. To perform mathematical operations
D. To control program flow
Q. What is the data type of the variable "name" in the following code snippet: String name = "John";
A. intB. double
C. boolean
D. String
Q. What is the correct way to declare an integer variable in Java?
A.int x = 5;B.
Integer x = 5;C.
Int x = 5;D.
x = 5;