Data Types & Variables MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. How many bits does an int occupy in Java?

    1. 32
    2. 16
    3. 64
    4. It depends on the platform
    Show answer

    Answer: A. 32

    The Java Language Specification fixes int at 32 bits (two's complement) on every platform, giving a range of -2,147,483,648 to 2,147,483,647. This fixed size is a key difference from C++, where int size is implementation-defined.

  2. What is the size of the char type in Java?

    1. 8 bits
    2. 32 bits
    3. 16 bits
    4. 1 bit
    Show answer

    Answer: C. 16 bits

    A Java char is a 16-bit unsigned UTF-16 code unit, range 0 to 65,535. C and C++ char is 1 byte (usually 8 bits), which is a common cross-language confusion.

  3. Which size rule is actually guaranteed by the C++ standard?

    1. int is always 32 bits
    2. long is always 64 bits
    3. double is always 4 bytes
    4. sizeof(char) is always 1
    Show answer

    Answer: D. sizeof(char) is always 1

    C++ defines sizeof(char) as exactly 1 byte. The standard only sets minimum ranges for other types: int must be at least 16 bits and long at least 32, so their sizes vary between compilers and platforms. Most modern desktop compilers use a 32-bit int.

  4. How do Python programmers normally mark a value as a constant?

    1. By naming it in UPPER_CASE, a convention the interpreter does not enforce
    2. With the const keyword
    3. With the final keyword
    4. By declaring it inside a constant block
    Show answer

    Answer: A. By naming it in UPPER_CASE, a convention the interpreter does not enforce

    Python has no const or final keyword for variables. PEP 8 recommends UPPER_CASE names such as MAX_MARKS to signal 'do not change', but the interpreter will still allow reassignment.

  5. Which Java keyword makes a variable's value unchangeable after it is assigned?

    1. const
    2. static
    3. final
    4. immutable
    Show answer

    Answer: C. final

    final prevents reassignment of a variable once it has a value, for example final double PI = 3.14159;. const is a reserved word in Java but is not used, and static means the field belongs to the class, not that it is constant.

  6. What happens when this C++ code is compiled?

    const int x = 5;
    x = 6;
    1. It compiles and x becomes 6
    2. It compiles, but x stays 5 at run time
    3. Compile error: assignment of a read-only variable
    4. It throws an exception at run time
    Show answer

    Answer: C. Compile error: assignment of a read-only variable

    A const variable cannot be modified after initialisation, so the compiler rejects x = 6; with an error such as "assignment of read-only variable 'x'". The check happens at compile time, not run time.

  7. What is the range of the Java byte type?

    1. -128 to 127
    2. 0 to 255
    3. -127 to 127
    4. -256 to 255
    Show answer

    Answer: A. -128 to 127

    byte is an 8-bit signed two's-complement integer. With 8 bits the range is -2^7 to 2^7 - 1, which is -128 to 127. 0 to 255 would be an unsigned byte, which Java does not have.

  8. Which of these is NOT a primitive type in Java?

    1. boolean
    2. char
    3. String
    4. double
    Show answer

    Answer: C. String

    Java has eight primitives: byte, short, int, long, float, double, char and boolean. String is a class in java.lang, so a String variable holds a reference to an object.

  9. What is the largest value a Python 3 int can hold?

    1. 2,147,483,647
    2. 9,223,372,036,854,775,807
    3. 1.8 × 10^308
    4. There is no fixed limit; it is bounded only by available memory
    Show answer

    Answer: D. There is no fixed limit; it is bounded only by available memory

    Python 3 int is arbitrary precision, so 2 ** 100 is computed exactly without overflow. The other values are the limits of a 32-bit signed int, a 64-bit signed long and the largest double.

  10. Python is described as dynamically typed. What does this mean?

    1. Every variable must be declared with a type before use
    2. Values are converted automatically between any types, so "5" + 3 gives 8
    3. Types are fixed at compile time and cannot change
    4. Types belong to values and are checked at run time, so a name can be rebound to a value of a different type
    Show answer

    Answer: D. Types belong to values and are checked at run time, so a name can be rebound to a value of a different type

    In Python, x = 5 then x = 'five' is legal because the name is not tied to a type; the type is a property of each object and is checked when operations run. Python is still strongly typed: "5" + 3 raises TypeError rather than converting silently.

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.