9th pratice

 

 MCQs on Variables and Data Types

1. What will be the output of the following code?

int a = 5, b = 2;

double c = a / b;

System.out.println(c);

a) 2
b) 2.0
c) 2.5
d) 2.50
Answer: b) 2.0


2. Which of the following is NOT a valid variable name?
a) _count
b) $total
c) 2value
d) value2
Answer: c) 2value


3. The range of a byte data type in Java is:
a) -128 to 127
b) 0 to 255
c) -255 to 255
d) -127 to 127
Answer: a) -128 to 127


4. What is the output of the following code?

char ch = 'A' + 1;

System.out.println(ch);

a) A
b) B
c) 66
d) Error
Answer: b) B


5. What is the result of 10 + 20 + "Java"?
a) Java30
b) 30Java
c) 1020Java
d) Error
Answer: b) 30Java


6. Which of the following statements is valid?
a) float f = 7.3;
b) float f = 7.3f;
c) float f = "7.3";
d) float f = (float)"7.3";
Answer: b) float f = 7.3f;


7. What is the default value of a local variable in Java?
a) 0
b) null
c) false
d) No default value
Answer: d) No default value


8. Which of the following is a reference data type?
a) int
b) float
c) char
d) String
Answer: d) String


9. What will be the output?

System.out.println(5 + 5 + "5" + 5 + 5);

a) 55555
b) 10555
c) 105510
d) 10555
Answer: d) 10555


10. Which of the following statements is true?
a) A boolean can store 0 or 1
b) A boolean can store only true or false
c) A boolean can store any number
d) None of the above
Answer: b) A boolean can store only true or false


11. What is the size of a long variable in Java?
a) 2 bytes
b) 4 bytes
c) 8 bytes
d) 16 bytes
Answer: c) 8 bytes


12. What will be the output?

int x = 5;

int y = ++x + x++;

System.out.println(y);

a) 10
b) 11
c) 12
d) 13
Answer: d) 13


13. Which of the following literals represents a character?
a) 'A'
b) "A"
c) A
d) 'A"
Answer: a) 'A'


14. What is the output?

double a = 10 / 4;

System.out.println(a);

a) 2
b) 2.0
c) 2.5
d) 2.50
Answer: b) 2.0


15. Which of the following conversions is not allowed automatically (implicit casting)?
a) int → long
b) char → int
c) double → float
d) byte → int
Answer: c) double → float


16. What is the data type of the result in:

25 / 4.0

a) int
b) float
c) double
d) long
Answer: c) double


17. Which variable declaration is valid?
a) int class = 10;
b) float _price = 12.5f;
c) boolean 1flag = true;
d) char "ch" = 'A';
Answer: b) float _price = 12.5f;


18. What is the output?

char ch = '9';

int n = ch - '0';

System.out.println(n);

a) 9
b) 0
c) 57
d) Error
Answer: a) 9


19. Which of these can store a decimal value?
a) int
b) long
c) float
d) char
Answer: c) float


20. What is the data type of the following literal?

12.34

a) float
b) double
c) long
d) int
Answer: b) double



✍️ 2-Mark Definition Questions

1. Define a variable in Java.
Answer:
A variable in Java is a named memory location used to store data that can change during program execution.


2. Define a data type in Java.
Answer:
A data type defines the kind of data a variable can hold, such as integer, floating-point number, character, or boolean.


3. What is meant by implicit type conversion?
Answer:
Implicit type conversion, also called type promotion, occurs when Java automatically converts a smaller data type into a larger one during an operation (e.g., int → double).


4. What is explicit type casting?
Answer:
Explicit type casting means manually converting one data type into another using a cast operator, for example:

double x = (double)5/2;


5. Define identifier with an example.
Answer:
An identifier is the name used to identify a variable, class, or method in Java.
Example: int marks; — here marks is an identifier.


6. Define literal in Java.
Answer:
A literal is a fixed value that does not change during program execution, such as 10, 'A', "Hello", or true.


7. What is a constant in Java?
Answer:
A constant is a variable whose value cannot be changed once assigned, declared using the keyword final.
Example: final int MAX = 100;


8. Define primitive data types in Java.
Answer:
Primitive data types are the basic data types provided by Java for storing simple values. They are: byte, short, int, long, float, double, char, and boolean.


9. What is the difference between integer division and floating-point division?
Answer:

  • Integer division: Both operands are integers; the result is an integer (e.g., 5 / 2 = 2).
  • Floating-point division: At least one operand is float/double; the result includes a fraction (e.g., 5.0 / 2 = 2.5).

10. Define type casting and give an example.
Answer:
Type casting is the process of converting a variable from one data type to another.
Example:

int x = (int) 5.8; // x becomes 5


💻 Hard Programmatic Question

Question:
Predict the output of the following Java program and explain how it works.

public class TestDataType {

    public static void main(String[] args) {

        int a = 5;

        double b = 2.5;

        char c = 'A';

        int result = (int)(a + b) + c;

        double finalValue = (result / 3) + (a % 2) * 1.5;

        System.out.println("Result = " + result);

        System.out.println("Final Value = " + finalValue);

    }

}

Write a Java program to read three numbers and display the largest number only if the difference between the largest and smallest number is even, otherwise display their average (as a decimal).

Comments

Popular posts from this blog

class 8 computer chapter 5

class 7 last chapter

CLASS 8 COMPUTER