Think you know C++ inside out? Dive into our Ultimate C++ Quiz: Master Fundamentals & Test Your Skills and see how you fare on this quiz in C++. This free c++ quiz challenges your grasp of variables, loops, conditional statements, and logic puzzles while offering instant feedback to turbocharge your learning. Whether you're a coding newbie eager to tackle a quiz in C++ or an experienced dev sharpening techniques, each question helps you learn and test core concepts. Perfect for coders seeking a quick cpp online test or prepping for technical interviews, you'll refine your problem-solving edge. Ready to elevate your code? Jump into our C++ MCQ quiz or try the swift c++ test - start now and boost your confidence!
Which header file is required to use std::cout and std::cin?
You must include to use std::cin and std::cout, since they are defined in that header file. This header provides the necessary declarations for input and output stream objects. Without it, the compiler will report undefined identifiers for cin and cout. Learn more.
Which operator is used to obtain the address of a variable?
->
&
%
*
The ampersand (&) operator in C++ is the address-of operator and returns the memory address of its operand. It is different from the indirection operator (*) which dereferences a pointer. Using &x yields a pointer to x. Learn more.
What is the correct way to declare a variable age of type int and initialize it to 30?
int age == 30;
age int = 30;
int age = 30;
int 30 = age;
In C++, the syntax int age = 30; declares an integer variable named age and initializes it to 30. The double equals (==) is a comparison operator, not assignment. Variable names cannot start with digits, and the type must precede the identifier. Learn more.
Which keyword indicates that a function does not return a value?
int
void
return
null
The keyword void specifies that a function does not return a value to the caller. It appears before the function name in the declaration and definition. Using int or other types indicates a return value type, and return is a statement to exit a function. Learn more.
What is the result of the expression 10 % 3?
2
1
3
0
The % operator computes the remainder of integer division. For 10 % 3, dividing 10 by 3 yields a quotient of 3 and a remainder of 1. Therefore the expression evaluates to 1. Learn more.
How do you denote a single-line comment in C++?
/* */
#
//
\\
Single-line comments in C++ start with // and continue to the end of the line. The /* */ syntax is used for block comments spanning multiple lines. A backslash or hash are not comment markers in standard C++. Learn more.
Which of the following is a correct way to declare a constant variable?
#define MAX = 100
const int MAX = 100;
int MAX
constant int MAX = 100;
In C++, the const keyword applied to a variable like const int MAX = 100; makes MAX immutable. The #define directive is a preprocessor macro, not a typed constant. There is no keyword 'constant' in C++, and 'int MAX' without initialization is not constant. Learn more.
Which of these is the entry point of a C++ program?
MyClass()
BEGIN()
main()
start()
Every standard C++ program starts execution from the function named main(). Other function names are not recognized by the runtime as the entry point. The signature may vary but the name must be main. Learn more.
Which loop structure will execute its body at least once regardless of the condition?
while loop
do-while loop
for loop
switch statement
A do-while loop executes its body first and then checks the condition, guaranteeing at least one execution. For and while loops check the condition before entering the loop, and switch is not a loop. Learn more.
What is the effect of the 'continue' statement inside a loop?
Restarts the program
Pauses execution
Skips the rest of the current iteration and proceeds to the next
Terminates the loop
The continue statement skips the remaining statements in the current iteration and transfers control to the loop's next iteration. It does not terminate the loop - break does that. It only affects the loop in which it is placed. Learn more.
Which of the following correctly checks if x is between 1 and 10 inclusive?
if(1 <= x || x <= 10)
if(x >= 1 && x <= 10)
if(1 < x < 10)
if(x > 1 && 10 > x)
In C++, chained comparisons like 1 < x < 10 are not valid logic for ranges. The correct way is to combine two comparisons with the logical AND operator: x >= 1 && x <= 10. The OR operator would allow values outside the intended range. Learn more.
Which operator is used to allocate dynamic memory for a single object in C++?
create
new
alloc
malloc
In C++, the new operator allocates memory from the free store and invokes constructors for object initialization. malloc is a C library function that does not call constructors. There is no create or alloc keyword in C++ for this purpose. Learn more.
Given int *p;, what does *p represent?
A pointer to an int
The size of int
The address of p
The value at the address pointed to by p
The unary * operator when applied to a pointer variable p dereferences it, yielding the value stored at the memory location p points to. The address-of operator (&) returns the address, not *. Learn more.
What does the & symbol represent when used in a function parameter like void func(int &x)?
Bitwise AND operator
Logical AND operator
Reference parameter
Address-of operator
In a function parameter, the ampersand (&) declares x as a reference, allowing the function to operate on the original argument rather than a copy. This is different from the address-of operator; context matters. Learn more.
Which feature allows functions with the same name but different parameters?
Inheritance
Encapsulation
Function overloading
Polymorphism
Function overloading lets you declare multiple functions with the same name but different parameter lists in the same scope. The compiler differentiates them by their signature. This is a form of compile-time polymorphism. Learn more.
What is the correct way to declare a function template?
template<> T max(T a, T b);
template T max(T a, T b);
template T max(a, b);
template T max(T a, T b);
Function templates require the template keyword with a parameter declaration like typename T or class T. template T max(T a, T b); is the proper syntax. A template<> is for explicit specialization, and omitting the type parameter list is invalid. Learn more.
Why should a base class have a virtual destructor when it's used polymorphically?
To prevent object slicing
To allow the derived class destructor to be called correctly
To speed up object destruction
To enable pure virtual functions
When deleting an object through a base-class pointer, a non-virtual destructor calls only the base destructor, leading to resource leaks. Declaring the base destructor virtual ensures the derived class destructor is invoked properly. This is essential for correct cleanup in polymorphic use. Learn more.
What is undefined behavior in C++?
Always throws an exception
Behavior not predictable or defined by the language
Guaranteed crash
Runs with no performance guarantees
Undefined behavior occurs when the C++ standard imposes no requirements on program execution, so anything can happen, including crashes or silent data corruption. The compiler may optimize based on the assumption UB does not occur. Safe coding avoids UB for predictable results. Learn more.
In what order are constructor calls for class members executed?
Reverse order of declaration
Order of declaration in class definition
Depends on the compiler
Alphabetical order
Class member constructors are called in the order in which members are declared within the class, regardless of the order in the initializer list. This guarantees a consistent initialization order. Deviating from this can lead to subtle bugs. Learn more.
What does RAII stand for in C++ and what is its main purpose?
Resource Acquisition Is Initialization
Random Access Is Initialization
Resource Allocation Is Immediate
Runtime Allocation Is Initialization
RAII binds resource management to object lifetime so that acquisition happens during construction and release happens in the destructor. This pattern ensures exception safety and prevents resource leaks. It is foundational for modern C++ resource handling. Learn more.
What is the effect of declaring a member function as const?
It makes the function return a const value
The function cannot modify any non-mutable member variables
It ensures the function runs in constant time
It prevents the function from being overloaded
A const member function promises not to modify the object's state, except for members declared mutable. It can be called on const instances of the class. Violating const in the function body results in a compile-time error. Learn more.
What is the default access specifier for members of a struct in C++?
public
private
internal
protected
In C++, struct members are public by default, whereas class members default to private. This subtle difference influences encapsulation and interface design. Using struct for plain data aggregates is idiomatic. Learn more.
Which smart pointer type can lead to cyclic references and memory leaks if not handled carefully?
unique_ptr
shared_ptr
auto_ptr
weak_ptr
shared_ptr uses reference counting to manage object lifetime. If two shared_ptr instances refer to objects that own shared_ptr back to each other, the count never drops to zero, causing leaks. weak_ptr avoids this by holding non-owning references. Learn more.
How do you capture the 'this' pointer in a lambda expression?
[=]
[this]
[ptr]
[&]
In C++11 and later, you capture the current object's this pointer using [this] in the lambda capture list. This lets the lambda access member variables and functions. Capturing by [=] or [&] does not include this unless explicitly supported in C++17. Learn more.
What does SFINAE stand for in C++ template metaprogramming?
Substitute Failures In Name And End
Substitution Failure Is Not An Error
Syntax Failure Is Normal At Execution
Single Function Is Named After Exception
SFINAE stands for Substitution Failure Is Not An Error. It allows the compiler to discard template overloads when type substitution fails, rather than producing an error. This is key for advanced template techniques and overload resolution. Learn more.
What is perfect forwarding in C++, and which operator is commonly used to implement it?
Using std::forward and && to preserve value category
Using & to forward by reference
Using * to dereference function arguments
Using const to protect parameters
Perfect forwarding preserves the value category (lvalue/rvalue) of arguments when passing them through function templates. It is typically implemented with && (universal references) and std::forward(arg). This enables efficient generic code. Learn more.
Which memory order enforces sequential consistency in C++11 atomic operations?
memory_order_seq_cst
memory_order_relaxed
memory_order_release
memory_order_acquire
memory_order_seq_cst is the strongest memory ordering, enforcing a total global order on operations for sequential consistency. It ensures all threads observe atomic operations in the same order. Weaker orders like acquire/release do not guarantee this global sequence. Learn more.
0
{"name":"Which header file is required to use std::cout and std::cin?", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"Which header file is required to use std::cout and std::cin?, Which operator is used to obtain the address of a variable?, What is the correct way to declare a variable age of type int and initialize it to 30?","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}
Score8/27
Easy4/8
Medium2/8
Hard2/8
Expert0/3
AI Study Notes
Email these to me
You can bookmark this page to review your notes in future, or fill out the email box below to email them to yourself.
Study Outcomes
Utilize Basic C++ Constructs -
Understand fundamental syntax and data types to write valid C++ code.
Evaluate Conditional Statements -
Analyze and predict the outcome of if, else-if, and switch-case logic.
Interpret Logical Expressions -
Apply logical operators such as &&, ||, and ! to build and evaluate complex expressions.
Solve Arithmetic and Assignment Problems -
Execute, interpret, and debug basic operations for accurate results.
Assess Performance with the C++ Quiz -
Review your quiz in C++ results to pinpoint strengths and identify areas for improvement.
Enhance Interview Readiness -
Leverage insights from this cpp online test to sharpen your skills for technical interviews.
Cheat Sheet
Primitive Data Types & Variable Scope -
C++ offers built-in types like int, double, char, and bool with size and range defined by ISO/IEC 14882 standards. Use the mnemonic "GIGA" (Global, Instance, Guarded, Automatic) to recall storage durations. Check cplusplus.com for examples such as double pi = 3.14; and bool ready = true;.
Arithmetic Operators & Precedence -
Operator precedence in C++ follows a hierarchy where multiplication, division, and modulo operators have higher precedence than addition and subtraction. Remember the PEMDAS rule (though exponentiation isn't built-in) and use parentheses to enforce evaluation order explicitly. Consult cppreference.com's operator precedence table when in doubt, e.g., (3 + 4) * 2 == 14.
Conditional Logic: if-else & switch -
The if/else if/else constructs let you branch code based on boolean conditions, while switch offers efficient matching on integral constants. A handy mnemonic is "Test, True, Else, Next" to follow the evaluation sequence. For deep dives and pattern examples, see Stroustrup's The C++ Programming Language or official ISO documents.
Logical Operators & Short-Circuiting -
Logical operators && (AND), || (OR), and ! (NOT) form complex boolean expressions evaluated left-to-right with short-circuiting. The "Short is Sweet" trick reminds you that C++ stops evaluating as soon as the outcome is determined: in false && x, x isn't checked. Lippman's C++ Primer and cppreference.com provide full truth-table examples.
Loops & Iteration Patterns -
Mastering for, while, and do-while loops is key to processing sequences and collections safely. Use the "ICBU" mnemonic (Initialize, Check, Body, Update) to trace execution and avoid off-by-one errors, e.g., for(int i=0; i<5; ++i). University courses like Stanford's CS106L and GeeksforGeeks offer pattern libraries and best practices.