MCQs on Data Types and Variables | Java Data Handling

Topic 1: Primitive Data Types

  1. Which of the following is not a primitive data type in Java?
    • A) int
    • B) double
    • C) String
    • D) boolean
  2. What is the default value of an int in Java?
    • A) 0
    • B) null
    • C) undefined
    • D) -1
  3. Which of the following primitive data types is used to represent decimal values in Java?
    • A) int
    • B) float
    • C) char
    • D) boolean
  4. What is the size (in bytes) of a char in Java?
    • A) 1 byte
    • B) 2 bytes
    • C) 4 bytes
    • D) 8 bytes
  5. Which of the following is the correct way to declare a boolean variable in Java?
    • A) boolean isActive = 1;
    • B) boolean isActive = true;
    • C) boolean isActive = “true”;
    • D) boolean isActive = 0;
  6. The default value of a boolean type in Java is:
    • A) true
    • B) false
    • C) null
    • D) 0
  7. Which of the following data types can store a large integer in Java?
    • A) int
    • B) long
    • C) short
    • D) byte
  8. Which primitive data type has the smallest range in Java?
    • A) byte
    • B) short
    • C) int
    • D) long
  9. How many bytes does a float consume in Java?
    • A) 2 bytes
    • B) 4 bytes
    • C) 8 bytes
    • D) 16 bytes
  10. Which primitive type is used to represent a single character in Java?
    • A) byte
    • B) char
    • C) int
    • D) String

Topic 2: Non-Primitive Data Types

  1. Which of the following is a non-primitive data type in Java?
  • A) int
  • B) char
  • C) String
  • D) byte
  1. Arrays are considered to be:
  • A) Primitive data types
  • B) Non-primitive data types
  • C) Immutable data types
  • D) Reference data types
  1. Which of the following is true about a String in Java?
  • A) Strings are mutable
  • B) Strings are non-primitive
  • C) Strings are a primitive data type
  • D) Strings do not support concatenation
  1. The StringBuilder class is used in Java for:
  • A) Creating immutable strings
  • B) Creating mutable strings
  • C) Working with primitive strings
  • D) Storing large amounts of data
  1. Which of the following is a correct way to declare a 2D array in Java?
  • A) int[][] arr = new int[2][3];
  • B) int arr[][] = new int[2, 3];
  • C) int[] arr = new int[2][3];
  • D) int arr[] = new int[2][3];
  1. Which of the following is the correct way to instantiate a String array in Java?
  • A) String[] arr = new String[5];
  • B) String arr[] = new String[5];
  • C) Both A and B
  • D) String[] arr = {“one”, “two”, “three”};
  1. What is the default value of an array element of type int in Java?
  • A) 0
  • B) null
  • C) undefined
  • D) 1
  1. Which of the following is not a valid object type in Java?
  • A) String
  • B) ArrayList
  • C) Integer
  • D) char
  1. Which of the following data types can hold a reference to a method in Java?
  • A) int
  • B) Function
  • C) Object
  • D) MethodType
  1. In Java, an object is created from a:
  • A) Class
  • B) Array
  • C) Primitive type
  • D) Method

Topic 3: Type Casting and Type Conversion

  1. Which type of casting is used when converting from a smaller data type to a larger data type in Java?
  • A) Implicit casting
  • B) Explicit casting
  • C) Automatic casting
  • D) Both A and C
  1. Which of the following is true about type casting in Java?
  • A) Only explicit casting can be done between primitive data types
  • B) Implicit casting can only be done between non-primitive data types
  • C) Implicit casting is done automatically by Java
  • D) Explicit casting requires the use of the convert() method
  1. Which of the following is an example of explicit casting in Java?
  • A) int i = 10; double d = i;
  • B) double d = 10.5; int i = (int) d;
  • C) byte b = 100; long l = b;
  • D) long l = 100L; double d = l;
  1. What happens when you try to explicitly cast a double to an int in Java?
  • A) The decimal part is retained
  • B) A runtime error occurs
  • C) The decimal part is truncated
  • D) The value is rounded
  1. Which of the following is a valid example of automatic (implicit) type conversion?
  • A) int i = 10; double d = i;
  • B) char c = ‘A’; int i = c;
  • C) short s = 100; long l = s;
  • D) All of the above
  1. Which of the following will cause a ClassCastException in Java?
  • A) Casting a double to an int
  • B) Casting an object of a subclass to its superclass
  • C) Casting an object of one class to an object of an incompatible class
  • D) None of the above
  1. Type conversion in Java is necessary when:
  • A) Converting a primitive data type to a reference data type
  • B) Converting incompatible data types
  • C) Storing a large number of values in an array
  • D) Passing a primitive value to a method
  1. In Java, what will happen if you try to assign a larger data type value to a smaller data type without explicit casting?
  • A) Compilation error
  • B) Data loss may occur
  • C) Runtime error
  • D) Implicit casting will occur
  1. Which method is used to convert a String to an int in Java?
  • A) Integer.parseInt()
  • B) String.valueOf()
  • C) Integer.toInt()
  • D) String.convertToInt()
  1. Which of the following is true about type casting between objects?
  • A) Objects of different classes cannot be cast to each other
  • B) Objects of compatible classes can be cast to each other
  • C) Only objects of the same class can be cast to each other
  • D) Type casting does not apply to objects

Answers

QnoAnswer
1C) String
2A) 0
3B) float
4B) 2 bytes
5B) boolean isActive = true;
6B) false
7B) long
8A) byte
9B) 4 bytes
10B) char
11C) String
12B) Non-primitive data types
13B) Strings are non-primitive
14B) Creating mutable strings
15A) int[][] arr = new int[2][3];
16C) Both A and B
17A) 0
18D) char
19C) Object
20A) Class
21A) Implicit casting
22C) Implicit casting is done automatically by Java
23B) double d = 10.5; int i = (int) d;
24C) The decimal part is truncated
25D) All of the above
26C) Casting an object of one class to an object of an incompatible class
27B) Converting incompatible data types
28B) Data loss may occur
29A) Integer.parseInt()
30B) Objects of compatible classes can be cast to each other

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top