Chapter 3 Beginner 57 Questions

Practice Questions — Variables, Data Types, and Type Casting

← Back to Notes
11 Easy
13 Medium
8 Hard

Topic-Specific Questions

Question 1
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 10;
    cout << x << endl;
    return 0;
}
A simple variable declaration and print.
10
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    double d = 3.5;
    int n = d;
    cout << n << endl;
    return 0;
}
What happens when a double is assigned to an int?
3
Question 3
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    char c = 'B';
    cout << (int)c << endl;
    return 0;
}
What is the ASCII value of 'B'?
66
Question 4
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    bool a = true;
    bool b = false;
    cout << a << " " << b << endl;
    return 0;
}
How does cout display bool values?
1 0
Question 5
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(int) << " " << sizeof(double) << endl;
    return 0;
}
Standard sizes on a 64-bit system.
4 8
Question 6
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 2147483647; // INT_MAX
    a = a + 1;
    cout << a << endl;
    return 0;
}
What happens when you exceed the maximum value of a signed int?
-2147483648 (on most systems, but technically undefined behavior)
Question 7
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    char c = 'A' + 3;
    cout << c << endl;
    return 0;
}
'A' is 65 in ASCII. What character is at 68?
D
Question 8
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    unsigned int u = 0;
    u = u - 1;
    cout << u << endl;
    return 0;
}
Unsigned integer subtraction wraps around.
4294967295
Question 9
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    auto x = 5;
    auto y = 5.0;
    cout << sizeof(x) << " " << sizeof(y) << endl;
    return 0;
}
auto deduces the type: 5 is int, 5.0 is double.
4 8
Question 10
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 100000;
    int b = 100000;
    long long result = a * b;
    cout << result << endl;
    return 0;
}
Both a and b are int. What precision is the multiplication done in?
1410065408 (not 10000000000)
Question 11
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    char c = '9';
    int digit = c - '0';
    cout << digit << endl;
    return 0;
}
'0' is 48 in ASCII, '9' is 57.
9
Question 12
Easy
What is the difference between int and long long in terms of size and range?
Think about 4 bytes vs 8 bytes.
int is 4 bytes with range approximately -2.1 x 10^9 to +2.1 x 10^9. long long is 8 bytes with range approximately -9.2 x 10^18 to +9.2 x 10^18. Use long long when values can exceed 2 x 10^9.
Question 13
Medium
What is the difference between const and constexpr?
Both prevent modification, but one guarantees compile-time evaluation.
const means the value cannot be changed after initialization, but the initial value may be computed at runtime. constexpr means the value must be computable at compile time and is embedded directly in the binary. constexpr implies const.
Question 14
Medium
Why does brace initialization int x{3.5}; cause a compiler error while int x = 3.5; does not?
Brace initialization was designed to be stricter.
Brace initialization (C++11) was specifically designed to prevent narrowing conversions -- conversions that lose data. Converting 3.5 (double) to int loses the .5, which is a narrowing conversion. Copy initialization (=) allows this silently for backward compatibility with C.
Question 15
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    float f = 1.1f;
    double d = 1.1;
    if (f == d) {
        cout << "Equal" << endl;
    } else {
        cout << "Not equal" << endl;
    }
    return 0;
}
float and double represent 1.1 with different precision.
Not equal
Question 16
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = -1;
    unsigned int y = 1;
    if (x < y) {
        cout << "-1 is less" << endl;
    } else {
        cout << "-1 is NOT less" << endl;
    }
    return 0;
}
When comparing signed and unsigned, the signed value is converted to unsigned.
-1 is NOT less
Question 17
Hard
What is the difference between a C-style cast (int)x and static_cast<int>(x)?
Think about type safety and what the compiler checks.
A C-style cast (int)x is unchecked -- it will attempt any conversion, even dangerous ones (like casting between unrelated pointer types). static_cast<int>(x) is checked at compile time and only allows conversions that make semantic sense (like numeric conversions). It refuses dangerous casts, making bugs easier to catch.
Question 18
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 7, b = 2;
    cout << a / b << endl;
    cout << (double)a / b << endl;
    return 0;
}
Integer division truncates. Casting one operand to double changes the division.
3
3.5
Question 19
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x;
    cout << sizeof(x) << endl;
    return 0;
}
sizeof works on variables too, not just types.
4
Question 20
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    char lower = 'g';
    char upper = lower - ('a' - 'A');
    cout << upper << endl;
    return 0;
}
'a' - 'A' is 32, the gap between lowercase and uppercase.
G

Mixed & Application Questions

Question 1
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 5, b = 10;
    double c = a + b;
    cout << c << endl;
    return 0;
}
int + int is int, then converted to double for storage.
15
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    char ch = 65;
    cout << ch << endl;
    return 0;
}
65 is the ASCII code for which character?
A
Question 3
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    double x = 9.99;
    int y = static_cast<int>(x);
    cout << y << endl;
    return 0;
}
static_cast from double to int truncates.
9
Question 4
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    bool a = 42;
    bool b = 0;
    bool c = -1;
    cout << a << " " << b << " " << c << endl;
    return 0;
}
Any non-zero value is true, zero is false.
1 0 1
Question 5
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    double y = 2.0;
    auto z = x + y;
    cout << z << " " << sizeof(z) << endl;
    return 0;
}
int + double promotes to double. auto deduces double.
7 8
Question 6
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    short s = 32767;
    s = s + 1;
    cout << s << endl;
    return 0;
}
32767 is the maximum value for a signed short (2 bytes).
-32768
Question 7
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(3 + 4.0) << endl;
    cout << sizeof('A' + 1) << endl;
    return 0;
}
3 + 4.0 promotes to double. 'A' + 1 promotes char to int.
8
4
Question 8
Easy
Write a C++ program that declares variables of types int, double, char, and bool, assigns values to them, and prints each with its sizeof.
Use sizeof(variable_name) for each variable.
#include <iostream>
using namespace std;
int main() {
    int age = 20;
    double gpa = 8.75;
    char grade = 'A';
    bool passed = true;
    cout << "age = " << age << ", size = " << sizeof(age) << endl;
    cout << "gpa = " << gpa << ", size = " << sizeof(gpa) << endl;
    cout << "grade = " << grade << ", size = " << sizeof(grade) << endl;
    cout << "passed = " << passed << ", size = " << sizeof(passed) << endl;
    return 0;
}
Question 9
Medium
Write a program that takes two integer inputs and prints their division result as a floating-point number (not truncated).
Cast one operand to double before dividing.
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    double result = static_cast<double>(a) / b;
    cout << result << endl;
    return 0;
}
Input: 7 2 Output: 3.5
Question 10
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 1000000;
    int y = 1000000;
    long long z = (long long)x * y;
    cout << z << endl;
    return 0;
}
The cast to long long happens before the multiplication.
1000000000000
Question 11
Medium
What is the auto keyword in C++11, and does it make C++ dynamically typed?
auto deduces the type at compile time, not runtime.
The auto keyword tells the compiler to deduce the variable's type from its initializer. auto x = 42; makes x an int. C++ is still statically typed -- the type is determined at compile time and cannot change. auto is syntactic convenience, not dynamic typing.
Question 12
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    const int N = 10;
    cout << N << endl;
    // N = 20;  // Would this work?
    return 0;
}
const prevents modification.
10

Multiple Choice Questions

MCQ 1
What is the size of int on most modern 64-bit systems?
  • A. 2 bytes
  • B. 4 bytes
  • C. 8 bytes
  • D. Depends on the compiler
Answer: B
B is correct. On virtually all modern systems (32-bit and 64-bit), int is 4 bytes (32 bits). The C++ standard only guarantees at least 16 bits, but in practice it is always 32 bits.
MCQ 2
Which data type should you use when values can reach 10^18?
  • A. int
  • B. long
  • C. long long
  • D. unsigned int
Answer: C
C is correct. long long is 8 bytes with a max of ~9.2 x 10^18. int (max ~2.1 x 10^9) and unsigned int (max ~4.3 x 10^9) are too small. long may be 4 or 8 bytes depending on the platform.
MCQ 3
What is the ASCII value of 'A' in C++?
  • A. 48
  • B. 65
  • C. 97
  • D. 90
Answer: B
B is correct. 'A' is 65. 'a' is 97. '0' is 48. 'Z' is 90. These ASCII values are fundamental and frequently tested.
MCQ 4
What does sizeof(char) always return in C++?
  • A. 0
  • B. 1
  • C. 2
  • D. It varies by platform
Answer: B
B is correct. The C++ standard guarantees that sizeof(char) is always exactly 1 byte on every platform and every compiler.
MCQ 5
What value does bool b = 42; assign to b?
  • A. 42
  • B. 0
  • C. 1 (true)
  • D. Compilation error
Answer: C
C is correct. Any non-zero integer converts to true (1) when assigned to bool. Only 0 converts to false (0).
MCQ 6
What happens with int x{3.5}; in C++11?
  • A. x becomes 3 (truncated)
  • B. x becomes 4 (rounded)
  • C. Compilation error: narrowing conversion
  • D. x becomes 3.5
Answer: C
C is correct. Brace initialization prevents narrowing conversions. Converting double 3.5 to int loses the 0.5, which is a narrowing conversion. The compiler rejects it with an error.
MCQ 7
What is the value of 'a' - 'A'?
  • A. 26
  • B. 32
  • C. 48
  • D. 97
Answer: B
B is correct. 'a' is 97 and 'A' is 65. The difference is 32. This constant gap applies to all letters: 'b' - 'B' is also 32, 'z' - 'Z' is also 32.
MCQ 8
Which of the following is the preferred explicit cast in modern C++?
  • A. (int)x
  • B. int(x)
  • C. static_cast<int>(x)
  • D. cast<int>(x)
Answer: C
C is correct. static_cast<int>(x) is the recommended explicit cast. It is type-safe (checked at compile time), searchable, and clearly documents intent. C-style cast (int)x and functional cast int(x) are less safe. cast<int> is not valid C++.
MCQ 9
What does constexpr int N = 10; guarantee?
  • A. N can be changed at runtime
  • B. N is computed at runtime but cannot be changed
  • C. N is computed at compile time and cannot be changed
  • D. N is only a hint to the compiler
Answer: C
C is correct. constexpr guarantees that the value is computed at compile time and embedded in the binary. It also implies const, so N cannot be modified.
MCQ 10
What is the output of cout << (int)('0');?
  • A. 0
  • B. 48
  • C. 65
  • D. Compilation error
Answer: B
B is correct. The character '0' (zero) has ASCII value 48. Casting it to int reveals this numeric value.
MCQ 11
What is the result of unsigned int u = -1; cout << u;?
  • A. -1
  • B. 0
  • C. 4294967295
  • D. Compilation error
Answer: C
C is correct. Assigning -1 to unsigned int performs modular arithmetic: -1 mod 2^32 = 4,294,967,295 (UINT_MAX). This compiles without error but is almost always a bug.
MCQ 12
In the expression int a = 100000; int b = 100000; long long c = a * b;, what is the value of c?
  • A. 10000000000
  • B. 1410065408
  • C. 0
  • D. Compilation error
Answer: B
B is correct. The multiplication a * b is performed in int precision (both operands are int). 10^10 overflows int, producing a garbage value (1410065408). The overflow happens BEFORE assignment to long long. Fix: (long long)a * b.
MCQ 13
What happens when comparing int x = -1; with unsigned int y = 0; using x < y?
  • A. true, because -1 < 0
  • B. false, because -1 converts to UINT_MAX
  • C. Compilation error
  • D. Undefined behavior
Answer: B
B is correct. When comparing signed and unsigned, the signed value is converted to unsigned. -1 becomes 4294967295 (UINT_MAX), which is greater than 0. So the comparison returns false. This is a well-known C++ pitfall.
MCQ 14
How many significant decimal digits of precision does float have?
  • A. ~3 digits
  • B. ~7 digits
  • C. ~15 digits
  • D. ~19 digits
Answer: B
B is correct. float (4 bytes, 32-bit IEEE 754) has approximately 7 significant decimal digits. double (8 bytes) has ~15 digits. long double has ~18-19 digits.
MCQ 15
What does auto x = 5.0f; deduce the type of x as?
  • A. int
  • B. double
  • C. float
  • D. long double
Answer: C
C is correct. The literal 5.0f has the f suffix, making it a float literal. Without the suffix, 5.0 would be double. auto deduces the exact type of the initializer.
MCQ 16
What is the default value of an uninitialized local int variable in C++?
  • A. 0
  • B. -1
  • C. Garbage (undefined)
  • D. NULL
Answer: C
C is correct. Local variables in C++ are NOT initialized. They contain whatever was previously in that memory location. Global and static variables are initialized to 0.
MCQ 17
Which initialization syntax prevents narrowing conversions?
  • A. int x = 3.5;
  • B. int x(3.5);
  • C. int x{3.5};
  • D. All of the above
Answer: C
C is correct. Brace initialization {} rejects narrowing conversions at compile time. int x{3.5} produces a compiler error because 3.5 cannot fit in int without data loss. Copy (=) and direct (()) initialization silently truncate.
MCQ 18
What is the size of long on a 64-bit Windows system using MSVC?
  • A. 4 bytes
  • B. 8 bytes
  • C. 16 bytes
  • D. Same as long long
Answer: A
A is correct. On Windows (MSVC), long is 4 bytes even on 64-bit systems (LLP64 model). On 64-bit Linux (GCC), long is 8 bytes (LP64 model). This platform difference is why long long is preferred for portability.
MCQ 19
What does sizeof(3.14) return?
  • A. 4
  • B. 8
  • C. 16
  • D. Depends on precision
Answer: B
B is correct. Floating-point literals without a suffix (like 3.14) are double by default in C++. sizeof(double) is 8 bytes. To get a float literal, use 3.14f.

Coding Challenges

Challenge 1: Temperature Converter

Easy
Write a C++ program that reads a temperature in Celsius (as a double) and converts it to Fahrenheit using the formula F = (C * 9/5) + 32. Print the result with the correct decimal value.
Sample Input
37.0
Sample Output
98.6
Use double for all calculations. Be careful with integer division: 9/5 is 1 in int arithmetic.
#include <iostream>
using namespace std;
int main() {
    double celsius;
    cin >> celsius;
    double fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
    cout << fahrenheit << endl;
    return 0;
}

Challenge 2: ASCII Table Printer

Easy
Write a program that prints the ASCII values of all uppercase letters (A-Z) and all digits (0-9). Format: 'A' = 65, 'B' = 66, etc.
Sample Input
(No input required)
Sample Output
'A' = 65 'B' = 66 ... 'Z' = 90 '0' = 48 ... '9' = 57
Use a for loop with char type. Cast to int to get the ASCII value.
#include <iostream>
using namespace std;
int main() {
    for (char c = 'A'; c <= 'Z'; c++) {
        cout << "'" << c << "' = " << (int)c << endl;
    }
    for (char c = '0'; c <= '9'; c++) {
        cout << "'" << c << "' = " << (int)c << endl;
    }
    return 0;
}

Challenge 3: Overflow Detector

Medium
Write a program that reads two integers a and b. Determine whether a * b would overflow an int. If it overflows, compute the result using long long and print it. If it fits in int, print it as int. Use INT_MAX from .
Sample Input
100000 100000
Sample Output
Overflow! Result (long long): 10000000000
Do not actually overflow. Check if (long long)a * b > INT_MAX or < INT_MIN before computing in int.
#include <iostream>
#include <climits>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    long long product = (long long)a * b;
    if (product > INT_MAX || product < INT_MIN) {
        cout << "Overflow! Result (long long): " << product << endl;
    } else {
        cout << "Fits in int: " << (int)product << endl;
    }
    return 0;
}

Challenge 4: Character Case Converter

Medium
Write a program that reads a single character. If it is an uppercase letter, convert it to lowercase. If it is a lowercase letter, convert it to uppercase. If it is neither, print it unchanged. Use ASCII arithmetic only (no toupper/tolower).
Sample Input
g
Sample Output
G
Use 'A'-'Z' range check and 'a'-'z' range check. The gap between uppercase and lowercase is 32.
#include <iostream>
using namespace std;
int main() {
    char c;
    cin >> c;
    if (c >= 'A' && c <= 'Z') {
        cout << (char)(c + 32) << endl;
    } else if (c >= 'a' && c <= 'z') {
        cout << (char)(c - 32) << endl;
    } else {
        cout << c << endl;
    }
    return 0;
}

Challenge 5: Digit Extractor

Medium
Write a program that reads a 5-digit integer and prints each digit on a separate line. Use modulus (%) and division (/) operators, not string conversion.
Sample Input
47329
Sample Output
4 7 3 2 9
Extract digits using repeated division by 10. Print from most significant to least significant.
#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    cout << n / 10000 << endl;
    cout << (n / 1000) % 10 << endl;
    cout << (n / 100) % 10 << endl;
    cout << (n / 10) % 10 << endl;
    cout << n % 10 << endl;
    return 0;
}

Challenge 6: Type Size Comparator

Hard
Write a program that demonstrates all three initialization styles (copy, direct, brace) for each fundamental type (int, double, char, bool). For each, print the variable value and its sizeof. Then show that brace initialization catches narrowing: attempt int x{3.14} in a comment and explain why it fails.
Sample Input
(No input required)
Sample Output
Copy: int a = 10, size = 4 Direct: int b(20), size = 4 Brace: int c{30}, size = 4 ...
Use all three initialization styles. Include a comment showing the narrowing error.
#include <iostream>
using namespace std;
int main() {
    // Copy initialization
    int a = 10;
    double d1 = 3.14;
    char c1 = 'A';
    bool b1 = true;

    // Direct initialization
    int a2(20);
    double d2(6.28);
    char c2('B');
    bool b2(false);

    // Brace initialization
    int a3{30};
    double d3{9.42};
    char c3{'C'};
    bool b3{true};

    cout << "Copy:   int=" << a << "(" << sizeof(a) << ") double=" << d1 << "(" << sizeof(d1) << ") char=" << c1 << "(" << sizeof(c1) << ") bool=" << b1 << "(" << sizeof(b1) << ")" << endl;
    cout << "Direct: int=" << a2 << "(" << sizeof(a2) << ") double=" << d2 << "(" << sizeof(d2) << ") char=" << c2 << "(" << sizeof(c2) << ") bool=" << b2 << "(" << sizeof(b2) << ")" << endl;
    cout << "Brace:  int=" << a3 << "(" << sizeof(a3) << ") double=" << d3 << "(" << sizeof(d3) << ") char=" << c3 << "(" << sizeof(c3) << ") bool=" << b3 << "(" << sizeof(b3) << ")" << endl;

    // int x{3.14};  // ERROR: narrowing conversion from double to int
    int x = 3.14;  // This compiles but truncates to 3
    cout << "Narrowed: " << x << endl;

    return 0;
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn C++ with a live mentor?

Explore our C++ Masterclass