Bird
Raised Fist0
C++programming~20 mins

Built-in data types in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Built-in Data Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of integer and floating-point operations
What is the output of the following C++ code?
C++
#include <iostream>
int main() {
    int a = 5;
    double b = 2.0;
    std::cout << a / b << std::endl;
    return 0;
}
A2
B2.50
C2.5
D2.0
Attempts:
2 left
💡 Hint
Remember that dividing an int by a double results in a double.
❓ Predict Output
intermediate
2:00remaining
Size of built-in data types
What is the output of this C++ code on a typical 64-bit system?
C++
#include <iostream>
int main() {
    std::cout << sizeof(char) << "," << sizeof(int) << "," << sizeof(long long) << std::endl;
    return 0;
}
A1,4,8
B1,8,8
C1,4,4
D2,4,8
Attempts:
2 left
💡 Hint
On most 64-bit systems, char is 1 byte, int is 4 bytes, and long long is 8 bytes.
🧠 Conceptual
advanced
2:00remaining
Understanding signed and unsigned integer behavior
What will be the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    unsigned int x = 0;
    std::cout << x - 1 << std::endl;
    return 0;
}
A4294967295
B0
C-1
DCompilation error
Attempts:
2 left
💡 Hint
Unsigned integers wrap around when subtracting beyond zero.
❓ Predict Output
advanced
2:00remaining
Boolean type and implicit conversion
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    bool flag = 5;
    std::cout << flag << std::endl;
    return 0;
}
A5
B1
Ctrue
D0
Attempts:
2 left
💡 Hint
Any non-zero integer assigned to a bool becomes true (1).
🔧 Debug
expert
2:00remaining
Identify the error in this code using built-in types
What error does this C++ code produce when compiled?
C++
#include <iostream>
int main() {
    int x = 10;
    double y = 3.5;
    int z = x / y;
    std::cout << z << std::endl;
    return 0;
}
ANo error, output is 3
BCompilation error: cannot assign double to int
CRuntime error: division by zero
DNo error, output is 2
Attempts:
2 left
💡 Hint
Division of int by double results in double, then assigned to int truncates the decimal.

Practice

(1/5)
1.

Which built-in data type in C++ is used to store whole numbers without decimals?

easy
A. int
B. double
C. char
D. bool

Solution

  1. Step 1: Understand data types for numbers

    Whole numbers without decimals are stored in integer types.
  2. Step 2: Identify the integer type in C++

    The int type stores whole numbers like 1, 2, -5.
  3. Final Answer:

    int -> Option A
  4. Quick Check:

    Whole numbers = int [OK]
Hint: Whole numbers use int type in C++ [OK]
Common Mistakes:
  • Choosing double for whole numbers
  • Using char for numbers
  • Confusing bool with numbers
2.

Which of the following is the correct way to declare a variable of type char in C++?

?
easy
A. char letter = 65
B. char letter = "A";
C. char letter = A;
D. char letter = 'A';

Solution

  1. Step 1: Understand char initialization

    Characters must be enclosed in single quotes in C++.
  2. Step 2: Check each option

    char letter = 'A'; uses single quotes correctly. char letter = "A"; uses double quotes (string). char letter = A; misses quotes. char letter = 65; uses an int, which is allowed but not typical for char.
  3. Final Answer:

    char letter = 'A'; -> Option D
  4. Quick Check:

    char uses single quotes for characters [OK]
Hint: Use single quotes for char values [OK]
Common Mistakes:
  • Using double quotes for char
  • Missing quotes around character
  • Assigning int without cast
3.

What will be the output of the following C++ code?

#include <iostream>
int main() {
    bool flag = 0;
    if (flag) {
        std::cout << "True";
    } else {
        std::cout << "False";
    }
    return 0;
}
medium
A. True
B. False
C. 0
D. 1

Solution

  1. Step 1: Understand bool initialization

    The variable flag is set to 0, which means false in boolean context.
  2. Step 2: Check the if condition

    Since flag is false, the else block runs, printing "False".
  3. Final Answer:

    False -> Option B
  4. Quick Check:

    bool 0 means false [OK]
Hint: bool 0 means false, else block runs [OK]
Common Mistakes:
  • Thinking 0 is true
  • Expecting numeric output
  • Confusing bool with int
4.

Identify the error in this C++ code snippet:

double price = 9.99;
char letter = "B";
bool isAvailable = true;
medium
A. Incorrect bool value assignment
B. Missing semicolon after double declaration
C. Using double quotes for char variable
D. No errors in the code

Solution

  1. Step 1: Check char variable initialization

    The variable letter is declared as char but assigned with double quotes, which is incorrect.
  2. Step 2: Verify other lines

    The double and bool declarations are correct with semicolons and proper values.
  3. Final Answer:

    Using double quotes for char variable -> Option C
  4. Quick Check:

    char needs single quotes, not double [OK]
Hint: char must use single quotes, not double quotes [OK]
Common Mistakes:
  • Using double quotes for char
  • Missing semicolons
  • Wrong bool literals
5.

You want to store a person's age, initial letter of their name, and whether they are a student. Which built-in data types should you use in C++?

medium
A. int for age, char for initial, bool for student status
B. double for age, string for initial, int for student status
C. char for age, int for initial, bool for student status
D. bool for age, char for initial, double for student status

Solution

  1. Step 1: Choose type for age

    Age is a whole number, so int is suitable.
  2. Step 2: Choose type for initial letter

    Initial is a single character, so char fits.
  3. Step 3: Choose type for student status

    Student status is yes/no, so bool is best.
  4. Final Answer:

    int for age, char for initial, bool for student status -> Option A
  5. Quick Check:

    Whole number = int, letter = char, yes/no = bool [OK]
Hint: Use int for numbers, char for letters, bool for yes/no [OK]
Common Mistakes:
  • Using double for age
  • Using string for single char
  • Using int for boolean values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard