Data Types & Variables MCQs for NSCT
Data types decide what values a variable can hold and which operations are allowed on them; variables are named storage whose lifetime and visibility are set by scope. This is the second topic in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 47.8% accuracy in this area, per the HEC NSCT report, and most lost marks here come from conversion and overflow questions.
Primitive types
A primitive type is a basic built-in type that holds a simple value directly, such as a number, character or Boolean. Java fixes every size in its specification; C++ only sets minimum ranges; Python has no primitives in the same sense, because every value is an object.
| Type | Java | C++ (typical) | Python 3 |
|---|---|---|---|
| Small integer | byte 8-bit, -128 to 127 |
char / short |
none |
| Integer | int 32-bit, fixed |
int at least 16-bit, usually 32 |
int, unlimited precision |
| Large integer | long 64-bit |
long long at least 64-bit |
same int |
| Real number | float 32-bit, double 64-bit |
same, usually | float (a 64-bit double) |
| Character | char 16-bit UTF-16 |
char 1 byte |
no char type; a 1-length str |
| Boolean | boolean, not numeric |
bool, converts to 0/1 |
bool, a subclass of int |
String is not a primitive in Java; it is a class, so a String variable holds a reference. A literal such as 3.14 is a double in Java and C++; write 3.14f for a float.
Exam angle: Java int overflow wraps silently: Integer.MAX_VALUE + 1 gives -2147483648 with no exception. Python ints never overflow.
Type conversion
Type conversion changes a value from one type to another. Implicit (automatic) conversion happens when no information can be lost, usually widening: double d = 10; stores 10.0. Explicit conversion, called a cast, is needed when narrowing, because data may be lost:
int a = (int) 9.99; // 9: the cast truncates toward zero
byte b = (byte) 130; // -126: only the low 8 bits are kept
char c = 'A';
System.out.println(c + 2); // 67: the char is promoted to int
Python converts with functions: int("42") gives 42, int(-3.99) gives -3, and int("3.5") raises ValueError. Python is dynamically typed (types belong to values, checked at run time) but strongly typed ("5" + 3 raises TypeError). JavaScript is weakly typed: "5" + 2 is "52" and "5" * 2 is 10.
Exam angle: the conversion happens after the arithmetic. In double avg = 7 / 2; in C++ or Java, integer division gives 3 first, so avg is 3.0.
Constants
A constant is a named value that should not change after it is set.
| Language | Keyword | What is fixed |
|---|---|---|
| C++ | const |
The value; reassignment is a compile error |
| Java | final |
The variable; for a reference, only which object it points to |
| Python | none | Nothing; UPPER_CASE names are a convention only |
So final int[] a = {1, 2, 3}; a[0] = 9; compiles in Java, because the array contents are not final, only the reference a is.
Exam angle: examiners like asking whether PI = 3 after PI = 3.14 is an error in Python. It is not; the name is simply rebound.
Scope
Scope is the region of code where a name can be used. C++ and Java use block scope: a variable declared inside { } disappears at the closing brace. Python uses function scope, resolved by the LEGB rule: Local, Enclosing, Global, Built-in.
count = 0
def add():
count += 1 # UnboundLocalError
Assigning to count inside add makes it local for the whole function, so the read in += fails. Declaring global count fixes it. JavaScript has both styles: let and const are block-scoped, while var is function-scoped.
Exam angle: Java fields get default values (0, false, null) but local variables do not; reading an unassigned local is a compile error.
Common traps
- Expecting
int(-3.99)to give -4. It truncates toward zero, giving -3. - Assuming Java
float f = 2.5;compiles.2.5is adouble, so it needs2.5f. - Forgetting that
long x = 24 * 60 * 60 * 1000 * 1000;overflows inintbefore it is widened tolong. - Believing Java
finalmakes an object immutable. It only fixes the reference. - Naming a variable
listorstrin Python, which hides the built-in function. - Treating C++ unsigned wrap-around (defined) like signed overflow (undefined behaviour).
Worked examples
1. Understanding. Which Java type should store a value such as 5,000,000,000?
Answer: long. An int holds at most 2,147,483,647, so the value needs the 64-bit long, written as the literal 5000000000L. Without the L suffix, the compiler rejects the literal as too large for an int.
2. Application. What does this Python code print?
a = "7"
b = 3
print(int(a) * b, a * b)
Answer: 21 777. int(a) converts the string to the number 7, so the first value is 21. a * b repeats the string three times, giving 777.
3. Analysis. A C++ student writes int total = 250; char grade = total; and prints (int) grade. On a typical compiler with a signed 8-bit char, why is the result not 250?
Answer: 250 does not fit in a signed 8-bit range (-128 to 127). Since C++20 the conversion is defined as keeping the low 8 bits, giving 250 - 256 = -6; older standards called the result implementation-defined, and common compilers already gave -6. The fix is to keep the value in an int.
Practise Data Types & Variables MCQs
The quiz below has 99 questions that you can filter by tier and difficulty.
Practise: 99 questions
FAQs
What is the difference between primitive and reference types?
A primitive variable stores its value directly, such as an int holding 5. A reference variable stores the address of an object, such as a String or an array in Java. Copying a primitive copies the value; copying a reference makes two variables point to the same object, so changes through one are visible through the other.
Is Python statically or dynamically typed?
Python is dynamically typed: you do not declare types, and a name can be rebound to a value of a different type. It is also strongly typed, because it refuses most implicit conversions between unrelated types, such as adding a string to an integer. Type hints exist, but the interpreter does not enforce them.
Why does 0.1 + 0.2 not equal 0.3?
float and double store numbers in binary, and 0.1 has no exact binary form, just as one third has no exact decimal form. The small rounding errors add up, so 0.1 + 0.2 == 0.3 is false in C++, Java, Python and JavaScript. Compare floats with a tolerance, or use decimal types for money.
What is the difference between implicit and explicit conversion?
Implicit conversion is done automatically by the language, usually when widening, such as int to double. Explicit conversion is requested by the programmer with a cast, such as (int) 9.99 in Java or int("42") in Python. Narrowing conversions normally need an explicit cast because information can be lost.
Next step
Continue with Operators & Expressions, the next topic in HEC's order, or go back to Programming Fundamentals. You can also browse all Programming MCQs or try a timed NSCT mock test.