programming
beginner
10 sample questions
Variables And Data Types MCQ Practice Test
Understanding how to declare variables and differentiate between types like int, float, string, and boolean.
Q1. Consider a programming language where integer variables are represented using 16 bits in two's complement format. What is the largest positive integer value that can be stored in such a variable, expressed in decimal?
-
A. 32767 ✓
-
B. 65535
-
C. 65536
-
D. -32768
Explanation: In a 16-bit two's complement system, the most significant bit represents the sign. Therefore, the largest positive integer is 2^15 - 1 = 32767. The range is -32768 to 32767.
Q2. Consider a Python program where you need to store the precise coordinates of a geographical location. Which data type would be MOST appropriate to represent a coordinate pair (latitude, longitude), ensuring both values maintain high precision and are easily manipulated together?
-
A. List of floats
-
B. Tuple of floats ✓
-
C. Dictionary with 'latitude' and 'longitude' keys holding float values
-
D. String containing latitude and longitude separated by a comma
Explanation: While a dictionary offers named access, a tuple is more suitable for representing a coordinate pair because it's immutable (preventing accidental modification of coordinates) and allows efficient simultaneous manipulation of both latitude and longitude. Lists are mutable, strings require parsing, making tuples the most efficient and robust choice for this specific scenario.
Q3. Consider the following Python code snippet:
python
x = 10
y = "10"
z = x + int(y)
What will be the data type of the variable 'z' after the execution of this code?
-
A. integer ✓
-
B. string
-
C. float
-
D. boolean
Explanation: Initially, 'x' is an integer and 'y' is a string. The `int()` function converts the string representation of '10' in 'y' to an integer. The addition operation then adds two integers (x and the converted y), resulting in an integer value stored in 'z'.
Q4. Consider the following Python code snippet:
python
string_var = "123"
int_var = int(string_var)
float_var = float(string_var)
result = int_var + float_var
print(type(result))
What will be the output of this code?
-
A. <class 'str'>
-
B. <class 'int'>
-
C. <class 'float'> ✓
-
D. <class 'bool'>
Explanation: The variable `string_var` is initially a string. `int(string_var)` converts it to an integer, and `float(string_var)` converts it to a float. Adding an integer and a float results in a float. Therefore, the `type(result)` will be `<class 'float'>`.
Q5. Consider the following Python code snippet:
python
x = 10
y = "hello"
z = x + y
What will be the output if you try to print the value of 'z'? Assume no error handling is implemented.
-
A. The value of x (10) will be printed.
-
B. The value of y ("hello") will be printed.
-
C. A TypeError will occur. ✓
-
D. The combined string "10hello" will be printed.
Explanation: In Python, you cannot directly concatenate an integer (x) with a string (y) using the '+' operator. This will result in a TypeError because the '+' operator performs different operations depending on the data types of its operands. To combine them, you would need to explicitly convert the integer to a string using str(x).
Q6. Consider a programming language where integers are represented using 32 bits and floating-point numbers use 64 bits. If you declare the variables "x = 2147483647" (an integer) and "y = 2147483648.0" (a floating-point number), which statement about the internal representation is MOST accurate?
-
A. Both x and y use the same number of bits in memory.
-
B. x will be stored using 32 bits, but y will require more than 64 bits due to its size.
-
C. x will cause an overflow error, while y will be stored correctly using 64 bits. ✓
-
D. Both x and y will be stored correctly, with x using 32 bits and y using 64 bits.
Explanation: The maximum value for a 32-bit signed integer is 2,147,483,647. Therefore, assigning 2147483647 to x is within the range, while 2147483648.0 exceeds this limit. However, floating point numbers can represent a wider range of values and have a different internal structure than integers. The floating-point number y can still be stored correctly using the 64 bits allocated for it.
Q7. Consider a programming language where integers are represented using 32 bits and floating-point numbers use a 64-bit double-precision format. Assuming a system uses IEEE 754 standard for floating-point representation, which of the following statements is TRUE regarding the potential for data loss when converting a 64-bit floating-point number to a 32-bit integer without intermediate type casting?
-
A. Data loss is impossible; the integer part of the floating-point number will always fit within a 32-bit integer.
-
B. Data loss is guaranteed; the precision loss inherent in the conversion will always result in information loss.
-
C. Data loss is possible; it occurs only if the floating-point number's absolute value exceeds the maximum representable 32-bit integer value. ✓
-
D. Data loss is possible; it occurs only if the floating-point number has a fractional part.
Explanation: While truncating the fractional part always leads to some form of precision loss in the numerical value, a 64-bit floating point number can represent integer values far larger than a 32-bit integer. Therefore, if the integer part of a floating-point number exceeds the range of a 32-bit signed integer (-2,147,483,648 to 2,147,483,647), conversion will lead to data loss (overflow).
Q8. Consider the following Python code snippet:
python
x = 10
y = "10"
z = x + int(y)
print(type(z))
What will be the output of this code, specifically the data type printed?
-
A. <class 'str'>
-
B. <class 'int'> ✓
-
C. <class 'float'>
-
D. <class 'bool'>
Explanation: The variable 'x' is an integer. The variable 'y' is a string representation of an integer. The `int()` function converts the string 'y' to an integer. The addition of 'x' and the converted 'y' results in an integer. Therefore, `type(z)` will return <class 'int'>.
Q9. Consider a programming language where integers are represented using 32 bits and floating-point numbers use 64 bits. A programmer declares the following variables: `int age = 2147483647;` and `double price = 999999999999999.99;`. Assuming a two's complement representation for integers, what will be the value of `age + 1` and the *closest* value of `price` stored in the computer's memory, respectively?
-
A. -2147483648, 1.000000000000000e+15 ✓
-
B. 2147483648, 1.000000000000000e+15
-
C. 0, 999999999999999.99
-
D. -2147483648, 1000000000000000
Explanation: In two's complement, adding 1 to the maximum integer value results in integer overflow, yielding the minimum integer value. Floating-point numbers have limited precision; the closest representable value to 999999999999999.99 may be slightly different due to rounding errors. The scientific notation 1.000000000000000e+15 shows the likely result of this rounding.
Q10. In a C++ program compiled for 64-bit Linux (LP64 data model), what is the size of a variable of type 'unsigned long'?
-
A. 4 bytes
-
B. 8 bytes ✓
-
C. 1 byte
-
D. 2 bytes
Explanation: The size of long depends on the platform's data model: LP64 systems (Linux, macOS, most Unix) use 8-byte longs, while 64-bit Windows uses LLP64, where long is only 4 bytes. Under the LP64 model named in the question, unsigned long is 8 bytes.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free