Chapter 2 Beginner 54 Questions

Practice Questions — Setting Up C++ and Your First Program

← Back to Notes
11 Easy
12 Medium
9 Hard

Topic-Specific Questions

Question 1
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello" << endl;
    cout << "World" << endl;
    return 0;
}
Each endl moves output to the next line.
Hello
World
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "A" << "B" << "C" << endl;
    return 0;
}
Chaining << outputs values sequentially without spaces.
ABC
Question 3
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Sum = " << 5 + 3 << endl;
    return 0;
}
Arithmetic expressions are evaluated before being printed.
Sum = 8
Question 4
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    // cout << "This is commented out";
    cout << "This prints" << endl;
    return 0;
}
Comments are ignored by the compiler.
This prints
Question 5
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello\nWorld\nC++" << endl;
    return 0;
}
\n is an escape sequence that produces a newline.
Hello
World
C++
Question 6
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 10;
    int y = 3;
    cout << x / y << endl;
    return 0;
}
Both operands are int, so this is integer division.
3
Question 7
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello";
    cout << " ";
    cout << "World";
    cout << endl;
    return 0;
}
Without endl or \n, outputs stay on the same line.
Hello World
Question 8
Easy
What is the difference between // and /* */ comments in C++?
One is for single lines, the other for multiple lines.
// is a single-line comment: everything after // until the end of the line is ignored. /* ... */ is a multi-line comment: everything between /* and */ is ignored, even across multiple lines.
Question 9
Medium
What does 'using namespace std;' do, and what happens if you omit it?
It relates to how C++ organizes standard library names.
using namespace std; imports all names from the std namespace into the current scope. If you omit it, you must prefix every standard library name with std::, like std::cout, std::cin, std::endl, std::string.
Question 10
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Tab:\tEnd" << endl;
    cout << "Quote: \"Hello\"" << endl;
    cout << "Backslash: \\" << endl;
    return 0;
}
\t is tab, \" is a literal double quote, \\ is a literal backslash.
Tab: End
Quote: "Hello"
Backslash: \
Question 11
Medium
Does this program compile? If yes, what is the output?
#include <iostream>
int main() {
    std::cout << 42 << std::endl;
    return 0;
}
Without 'using namespace std', you must use the std:: prefix.
Yes, it compiles. Output: 42
Question 12
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a, b;
    a = b = 5;
    cout << a << " " << b << endl;
    return 0;
}
Assignment operators are right-associative: b = 5 happens first, then a = b.
5 5
Question 13
Hard
Explain the difference between endl and '\n'. When would you prefer one over the other?
One does something extra that affects performance.
Both produce a newline, but endl also flushes the output buffer. Flushing forces all buffered output to be written to the screen immediately. '\n' just adds a newline without flushing. Use '\n' in competitive programming for better performance (flushing is slow when done millions of times). Use endl when you need to ensure output appears immediately (e.g., before waiting for input).
Question 14
Hard
Does this compile? What happens?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello" << endl
    return 0;
}
Look carefully at the end of the cout line.
It does NOT compile. Error: expected ';' before 'return'
Question 15
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    /* This is a
       multi-line comment */
    cout << 42 << endl;
    return 0;
}
Multi-line comments are completely ignored.
42
Question 16
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Line 1" << '\n' << "Line 2" << '\n';
    return 0;
}
'\n' works the same as endl for producing a newline (but does not flush).
Line 1
Line 2
Question 17
Medium
What is the g++ command to compile a file called 'solution.cpp' with C++17 standard and all warnings enabled, producing an executable called 'sol'?
Combine the -std, -Wall, and -o flags.
g++ -std=c++17 -Wall solution.cpp -o sol
Question 18
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 10;
    cout << "a" << endl;
    cout << a << endl;
    return 0;
}
Pay attention to the quotes. "a" is a string, a (without quotes) is a variable.
a
10
Question 19
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 10 / 3 << endl;
    cout << 10.0 / 3 << endl;
    return 0;
}
Integer division truncates. If either operand is floating-point, the result is floating-point.
3
3.33333
Question 20
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "First ";
    cout << "Second ";
    cout << "Third" << endl;
    return 0;
}
Without endl or \n, output continues on the same line.
First Second Third

Mixed & Application Questions

Question 1
Easy
Write a C++ program that prints 'Hello, I am [your name] and I am learning C++!' on one line.
Use cout with chained << operators.
#include <iostream>
using namespace std;
int main() {
    cout << "Hello, I am Vikas and I am learning C++!" << endl;
    return 0;
}
Output: Hello, I am Vikas and I am learning C++!
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 2 * 3 + 1 << endl;
    return 0;
}
Multiplication has higher precedence than addition.
7
Question 3
Medium
Write a C++ program that reads two integers from the user and prints their sum, difference, product, and quotient.
Use cin >> to read both numbers. Remember integer division truncates.
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cout << "Enter two integers: ";
    cin >> a >> b;
    cout << "Sum: " << a + b << endl;
    cout << "Difference: " << a - b << endl;
    cout << "Product: " << a * b << endl;
    cout << "Quotient: " << a / b << endl;
    return 0;
}
Input: 15 4 Output: Sum: 19 Difference: 11 Product: 60 Quotient: 3
Question 4
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    cout << x << endl;
    cout << "x" << endl;
    cout << 'x' << endl;
    return 0;
}
x (no quotes) is a variable, "x" is a string, 'x' is a character.
5
x
x
Question 5
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "A\tB\tC" << endl;
    cout << "1\t2\t3" << endl;
    return 0;
}
\t inserts a horizontal tab character.
A B C
1 2 3
Question 6
Medium
Write a program that reads a student's name (single word) and three subject marks, then prints the total and average.
Read the name with cin >>. Calculate average by dividing by 3.0 (not 3) for floating-point result.
#include <iostream>
using namespace std;
int main() {
    string name;
    int m1, m2, m3;
    cout << "Enter name: ";
    cin >> name;
    cout << "Enter 3 marks: ";
    cin >> m1 >> m2 >> m3;
    int total = m1 + m2 + m3;
    double avg = total / 3.0;
    cout << name << ": Total = " << total << ", Average = " << avg << endl;
    return 0;
}
Input: Divya 85 92 78 Output: Divya: Total = 255, Average = 85
Question 7
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 5 << " + " << 3 << " = " << 5 + 3 << endl;
    return 0;
}
Strings are printed literally, expressions are evaluated.
5 + 3 = 8
Question 8
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello" << endl;
    cout << "World" << endl;
    cout << endl;
    cout << "C++" << endl;
    return 0;
}
A bare endl by itself produces an empty line.
Hello
World

C++
Question 9
Hard
What happens if you write 'cout << "Hello";' but forget to include '#include '?
The compiler needs to know what cout is.
The program will NOT compile. The compiler will report: error: 'cout' was not declared in this scope. Without #include <iostream>, the compiler has no definition of cout, cin, endl, or the << / >> operators for streams.
Question 10
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 100 << " " << 200 << " " << 300 << endl;
    return 0;
}
Numbers and spaces are chained together.
100 200 300
Question 11
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 'A' << endl;
    cout << (int)'A' << endl;
    return 0;
}
Casting a char to int gives its ASCII value.
A
65
Question 12
Medium
What is the purpose of 'return 0;' in main()? What happens if you omit it?
It is the exit code sent to the operating system.
return 0; sends an exit code of 0 to the operating system, indicating the program ran successfully. If you omit return 0; in main(), C++ (since C++98) implicitly returns 0. So omitting it is valid -- the compiler inserts it automatically. However, explicitly writing it is considered good practice.

Multiple Choice Questions

MCQ 1
Which command compiles a C++ file called 'test.cpp' using g++?
  • A. gcc test.cpp
  • B. g++ test.cpp
  • C. cpp test.cpp
  • D. c++ test.cpp -compile
Answer: B
B is correct. g++ is the GNU C++ compiler. g++ test.cpp compiles the file and produces a default executable (a.out on Linux/Mac, a.exe on Windows). gcc (A) is for C. cpp (C) is the preprocessor. c++ -compile (D) is not valid syntax.
MCQ 2
What does #include do?
  • A. Imports the math library
  • B. Includes the input/output stream library for cout and cin
  • C. Includes the string manipulation library
  • D. Imports all standard libraries
Answer: B
B is correct. #include <iostream> includes the I/O stream library that provides cout, cin, cerr, clog, and endl. It does NOT include math functions (A), string functions (C), or all libraries (D).
MCQ 3
Which is the correct entry point for a C++ program?
  • A. void main()
  • B. int Main()
  • C. int main()
  • D. function main()
Answer: C
C is correct. The standard C++ entry point is int main(). void main() (A) is not standard-compliant. int Main() (B) is wrong because C++ is case-sensitive. function main() (D) is not valid C++ syntax.
MCQ 4
What character must appear at the end of every C++ statement?
  • A. Colon (:)
  • B. Period (.)
  • C. Semicolon (;)
  • D. Comma (,)
Answer: C
C is correct. Every statement in C++ must end with a semicolon ;. This is different from Python which uses newlines. The colon (A) is used in labels and class definitions. The period (B) and comma (D) have different purposes.
MCQ 5
Which operator is used with cin for input?
  • A. <<
  • B. >>
  • C. ->
  • D. >>
Answer: B
B is correct. cin >> uses the extraction operator >> to read input. Think of the arrows pointing from cin INTO the variable: data flows from the input stream to your variable. << (A) is the insertion operator used with cout.
MCQ 6
What is the difference between 'Hello' and "Hello" in C++?
  • A. They are identical
  • B. 'Hello' is a character and "Hello" is a string
  • C. 'Hello' is a multi-character literal (implementation-defined) and "Hello" is a string literal
  • D. 'Hello' causes a syntax error
Answer: C
C is correct. Single quotes are for single characters ('A'). 'Hello' is a multi-character literal, which has implementation-defined behavior (usually treated as an integer). Double quotes create string literals. Always use double quotes for strings.
MCQ 7
What does the -o flag do in 'g++ file.cpp -o myprogram'?
  • A. Optimizes the code
  • B. Opens the file for editing
  • C. Specifies the output executable name
  • D. Turns on all warnings
Answer: C
C is correct. The -o flag specifies the name of the output executable. Without it, the default is a.out (Linux/Mac) or a.exe (Windows). For optimization, use -O2 (capital O). For warnings, use -Wall.
MCQ 8
Which of the following is a valid single-line comment in C++?
  • A. # This is a comment
  • B. // This is a comment
  • C. -- This is a comment
  • D. ' This is a comment
Answer: B
B is correct. C++ uses // for single-line comments. # (A) is for preprocessor directives in C++, and comments in Python. -- (C) is for comments in SQL and Haskell. ' (D) is for comments in VB.
MCQ 9
What happens when cin >> reads a string and the user types 'Amit Kumar'?
  • A. The entire 'Amit Kumar' is stored
  • B. Only 'Amit' is stored, 'Kumar' stays in the buffer
  • C. An error occurs
  • D. Only 'Kumar' is stored
Answer: B
B is correct. cin >> reads until the first whitespace character (space, tab, newline). So only Amit is stored in the variable. Kumar remains in the input buffer and will be read by the next cin >> call. To read the full line, use getline(cin, variable).
MCQ 10
What does the -Wall flag do when compiling with g++?
  • A. Compiles for all platforms
  • B. Enables all common compiler warnings
  • C. Links all libraries
  • D. Creates a wall of text output
Answer: B
B is correct. -Wall stands for 'Warn all' and enables most common warnings: uninitialized variables, unused variables, implicit type conversions, etc. It does not actually enable ALL warnings (for that, also add -Wextra). Warnings help catch bugs before they cause problems.
MCQ 11
What is the output of: cout << endl << endl << endl;?
  • A. Three spaces
  • B. Three blank lines
  • C. The word 'endl' three times
  • D. Nothing (no output)
Answer: B
B is correct. Each endl outputs a newline character (and flushes the buffer). Three endl in sequence produce three newline characters, resulting in three blank lines in the output.
MCQ 12
Which of the following is NOT a valid way to use cout?
  • A. cout << 42;
  • B. cout << "hello" << endl;
  • C. cout << 'A';
  • D. cout >> "hello";
Answer: D
D is correct. cout uses the insertion operator <<, not the extraction operator >>. cout >> "hello" is invalid because >> is for extracting data FROM a stream (used with cin), not inserting into one.
MCQ 13
If you compile with 'g++ -std=c++17 program.cpp', what does -std=c++17 specify?
  • A. The program must have exactly 17 functions
  • B. Use the C++17 language standard for compilation
  • C. The output will be 17 MB or less
  • D. The program will run 17 times faster
Answer: B
B is correct. The -std=c++17 flag tells the compiler to use the C++17 standard. This enables C++17 features like structured bindings, if constexpr, std::optional, etc. Without this flag, the compiler uses its default standard (often C++14 or C++17 depending on the version).
MCQ 14
What does 'cout' stand for?
  • A. Count output
  • B. Character output
  • C. Console out
  • D. C output
Answer: B
B is correct. cout stands for character output. It is an object of the ostream class that sends character data to the standard output stream (usually the terminal/console).
MCQ 15
What does the escape sequence '\0' represent in C++?
  • A. The number zero
  • B. A newline character
  • C. The null character (string terminator)
  • D. An error
Answer: C
C is correct. \0 is the null character with ASCII value 0. It is used to mark the end of C-style strings (char arrays). Every string literal like "Hello" has a hidden \0 at the end. It is different from the digit 0 ('0', ASCII 48).
MCQ 16
Which IDE is free, lightweight, and recommended for beginners learning C++?
  • A. Visual Studio Enterprise
  • B. VS Code with C/C++ extension
  • C. IntelliJ IDEA
  • D. Eclipse for Java
Answer: B
B is correct. VS Code is free, lightweight, cross-platform, and has excellent C++ support through the C/C++ extension by Microsoft. It includes IntelliSense, debugging, and an integrated terminal. Visual Studio Enterprise (A) is paid. IntelliJ (C) and Eclipse for Java (D) are primarily Java IDEs.
MCQ 17
What is the default executable name when you compile with 'g++ program.cpp' (without -o flag)?
  • A. program.exe
  • B. program
  • C. a.out (Linux/Mac) or a.exe (Windows)
  • D. output.bin
Answer: C
C is correct. Without the -o flag, g++ produces a default-named executable: a.out on Linux/macOS and a.exe on Windows. The name a.out stands for 'assembler output' -- a convention dating back to the original Unix systems.
MCQ 18
Which of these is a valid escape sequence in C++?
  • A. \p
  • B. \t
  • C. \w
  • D. \z
Answer: B
B is correct. \t is the horizontal tab escape sequence. Valid escape sequences include \n (newline), \t (tab), \\ (backslash), \" (double quote), \' (single quote), \0 (null), and \a (bell). \p, \w, and \z are not valid and may cause compiler warnings.

Coding Challenges

Challenge 1: Rectangle Calculator

Easy
Write a C++ program that reads the length and width of a rectangle from the user and prints its area and perimeter.
Sample Input
5 3
Sample Output
Area: 15 Perimeter: 16
Use integer variables. Area = length * width. Perimeter = 2 * (length + width).
#include <iostream>
using namespace std;
int main() {
    int length, width;
    cout << "Enter length and width: ";
    cin >> length >> width;
    cout << "Area: " << length * width << endl;
    cout << "Perimeter: " << 2 * (length + width) << endl;
    return 0;
}

Challenge 2: Swap Two Numbers (Without Temp)

Medium
Write a program that reads two integers and swaps them using only arithmetic operators (no third variable). Print the values before and after swapping.
Sample Input
10 25
Sample Output
Before: a = 10, b = 25 After: a = 25, b = 10
Do NOT use a temporary variable. Use only arithmetic operations (+, -).
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    cout << "Before: a = " << a << ", b = " << b << endl;
    a = a + b;  // a = 35
    b = a - b;  // b = 35 - 25 = 10
    a = a - b;  // a = 35 - 10 = 25
    cout << "After: a = " << a << ", b = " << b << endl;
    return 0;
}

Challenge 3: Student Grade Card

Medium
Write a program that reads a student's name (single word) and marks in 5 subjects. Print the total marks, percentage, and average.
Sample Input
Meera 85 90 78 92 88
Sample Output
Name: Meera Total: 433 Percentage: 86.6% Average: 86.6
Use integer for marks and double for percentage/average. Maximum marks per subject is 100.
#include <iostream>
using namespace std;
int main() {
    string name;
    int m1, m2, m3, m4, m5;
    cin >> name >> m1 >> m2 >> m3 >> m4 >> m5;
    int total = m1 + m2 + m3 + m4 + m5;
    double percentage = total / 5.0;
    cout << "Name: " << name << endl;
    cout << "Total: " << total << endl;
    cout << "Percentage: " << percentage << "%" << endl;
    cout << "Average: " << percentage << endl;
    return 0;
}

Challenge 4: ASCII Art Box

Easy
Write a C++ program that prints a box made of + and - characters with a message inside: +----------+ | Hello! | +----------+
Sample Input
(No input required)
Sample Output
+----------+ | Hello! | +----------+
Use cout with escape sequences or multiple statements. Align the text inside the box.
#include <iostream>
using namespace std;
int main() {
    cout << "+----------+" << endl;
    cout << "|  Hello!  |" << endl;
    cout << "+----------+" << endl;
    return 0;
}

Challenge 5: Temperature Converter

Medium
Write a program that reads a temperature in Celsius from the user and converts it to Fahrenheit and Kelvin. Formula: F = (C * 9.0/5.0) + 32, K = C + 273.15.
Sample Input
37
Sample Output
Celsius: 37 Fahrenheit: 98.6 Kelvin: 310.15
Use double for all temperature variables to preserve decimal precision.
#include <iostream>
using namespace std;
int main() {
    double celsius;
    cout << "Enter temperature in Celsius: ";
    cin >> celsius;
    double fahrenheit = (celsius * 9.0 / 5.0) + 32;
    double kelvin = celsius + 273.15;
    cout << "Celsius: " << celsius << endl;
    cout << "Fahrenheit: " << fahrenheit << endl;
    cout << "Kelvin: " << kelvin << endl;
    return 0;
}

Challenge 6: Compilation Flags Practice

Hard
Write a C++ program that prints a welcome message. Then, in comments, write the exact g++ commands to: (1) compile with C++17, (2) compile with all warnings, (3) compile with optimization level 2, (4) compile and produce only the assembly output.
Sample Input
(No input required)
Sample Output
Welcome to C++ compilation practice!
Write all four compilation commands as comments at the top of the file.
// Compile with C++17: g++ -std=c++17 practice.cpp -o practice
// Compile with all warnings: g++ -Wall -Wextra practice.cpp -o practice
// Compile with optimization: g++ -O2 practice.cpp -o practice
// Produce assembly only: g++ -S practice.cpp -o practice.s

#include <iostream>
using namespace std;
int main() {
    cout << "Welcome to C++ compilation practice!" << 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