Variables & Constants

Variables in Java are named memory locations that hold data value of a particular data type. All variables must be declared before they can be used. Initialization is the process of providing value to a variable at declaration time. A variable is initialized once in its life time. To initialize a variable, type the assignment operator on the right side of its name, followed by the value. The names of variables in the Java language are referred to as identifiers. The scope of variables is declared within the block. Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared. All variables in the Java language must have a data type. A variable's type determines the values that the variable can have and the operations that can be performed on it.

A constant in Java is a variable whose value cannot be changed once it is initially bound to a value. Though a constant value is specified only once, the value can, however, be referenced multiple times within a program. The final keyword is used to declare a variable as constant.

To Access the full content, Please Purchase

  • Q1

    What is the error in the statement given below?

    int a=4.7

    Marks:1
    Answer:

    The semicolon is missing at the end to the statement to terminate the variable declaration.

    View Answer
  • Q2

    How can a variable salary containing a floating type value be declared?

    Marks:1
    Answer:

    float salary;

    View Answer
  • Q3

    Which keyword makes a variable constant?

    Marks:1
    Answer:

    final keyword makes a variable constant.

    View Answer
  • Q4

    Give the size of short data type.

    Marks:1
    Answer:

    The size of short data type is 16bits (or 2 bytes).

    View Answer
  • Q5

    What is a variable in Java?

    Marks:1
    Answer:

    A variable refers to a memory location that holds values in the computer memory.

    View Answer