Unlock hundreds more features
Save your Quiz to the Dashboard
View and Export Results
Use AI to Create Quizzes and Analyse Results

Sign inSign in with Facebook
Sign inSign in with Google

Master Your C/C++ Fundamentals Knowledge Test

Challenge Yourself with C/C++ Programming Basics Quiz

Difficulty: Moderate
Questions: 20
Learning OutcomesStudy Material
Colorful paper art depicting a quiz on CC Fundamentals Knowledge Test

Test your grasp of C/C++ fundamentals with this engaging, interactive quiz designed for aspiring programmers and seasoned developers alike. The C/C++ fundamentals quiz covers key concepts from syntax and pointers to memory management and object-oriented principles. Participants will gain insight into their strengths and uncover areas for improving coding skills. All questions can be freely modified in our editor to suit your learning goals. For a broader challenge, try the Technical Fundamentals Knowledge Test, explore the C# Fundamentals Quiz, or browse more quizzes.

Which header file is required to use the printf function in C?
stdlib.h
string.h
iostream
stdio.h
The printf function is declared in stdio.h. Including stdio.h provides the correct prototype for printf. Other headers like iostream are C++ - specific and do not declare printf.
What is the output of the following C code snippet? printf("%d", 10 / 4);
3
2.0
2.5
2
10 and 4 are integers, so integer division truncates toward zero, yielding 2. Decimal results are not produced when both operands are ints. Hence the output is 2.
How do you correctly declare a pointer to an integer in C or C++?
int* ptr;
ptr* int;
int ptr*;
pointer int ptr;
The syntax int* ptr; declares ptr as a pointer to int. The asterisk (*) associates ptr with the type int*. Other forms are invalid.
Which keyword terminates a loop immediately in C and C++?
break
stop
exit
continue
The break statement exits the nearest enclosing loop immediately. continue skips the rest of the loop body and proceeds with the next iteration. exit terminates the entire program.
In C++, which symbol is used to specify class inheritance?
# (hash)
:: (scope resolution)
: (colon)
-> (arrow)
In C++, inheritance is denoted by a colon after the derived class name (e.g., class B : public A {}). The :: operator is for scope resolution and -> is for pointer member access.
On most platforms, what is the size of the char data type in C and C++?
2 bytes
1 byte
4 bytes
It depends on the compiler
By definition, sizeof(char) is always 1 byte in both C and C++. This represents the basic addressable unit of memory. Other types are measured in multiples of this size.
What is the primary purpose of the malloc function in C?
Allocate a block of memory on the heap
Allocate memory on the stack
Deallocate previously allocated memory
Input/output operations
malloc reserves a contiguous block of memory on the heap and returns a pointer to it. It does not initialize the memory and must be paired with free to avoid leaks.
What happens if you use delete on memory allocated by malloc in C++?
The memory block becomes automatically resized
Correct deallocation of the memory
A compilation error
Undefined behavior because free must be used
Memory allocated with malloc must be freed with free. Using delete on malloc-allocated memory is undefined behavior and can corrupt the heap.
Which statement correctly describes a reference compared to a pointer in C++?
A reference can be null but a pointer cannot.
A reference occupies more memory than a pointer.
A reference requires explicit dereferencing with * while a pointer does not.
A reference is an alias that cannot be reseated, whereas a pointer can be changed to refer to different objects.
References are bound to an initial object and cannot be reseated. Pointers hold addresses and can be reassigned. By design, references cannot be null.
Which loop in C and C++ guarantees that its body is executed at least once?
goto
do-while
while
for
A do-while loop checks its condition after executing the loop body, ensuring at least one iteration. for and while loops test the condition before the first iteration.
What is the default access specifier for members of a struct in C++?
internal
public
protected
private
In C++, struct members are public by default. Conversely, class members default to private if no access specifier is provided.
How should you correctly free a dynamically allocated array created with new[] in C++?
Array will be freed automatically.
delete[] arrayPointer;
free(arrayPointer);
delete arrayPointer;
Arrays allocated with new[] must be deallocated with delete[]. Using delete without [] or free leads to undefined behavior or memory leaks.
Which C standard library function is used to copy a block of memory from source to destination?
strcat
memcpy
memset
strcpy
memcpy copies a specified number of bytes from a source to a destination. strcpy and strcat operate on null-terminated strings, and memset sets memory to a fixed value.
In C, which printf format specifier is used to print a pointer value?
%s
%p
%d
%x
The %p specifier is designed to print pointer addresses in an implementation-defined format. Other specifiers are intended for integers or strings.
What is the consequence of deleting a derived class object through a base class pointer when the base class destructor is not virtual in C++?
The derived class destructor is not called, risking resource leaks.
The program will not compile due to missing virtual destructor.
Both base and derived destructors are guaranteed to be called.
Only the derived destructor is called, skipping the base destructor.
Without a virtual destructor in the base class, delete invoked through a base pointer calls only the base destructor. The derived destructor is skipped, leading to potential resource leaks.
What is wrong with the following C++ code? int* arr = new int[5]; delete arr;
delete[] must be used only for single objects.
Using delete instead of delete[] causes undefined behavior for arrays.
Using delete[] here is illegal.
It will compile but produce a memory leak.
Arrays allocated with new[] require delete[] to properly invoke destructors and free memory. Using delete mismatches the allocation and results in undefined behavior.
In C, given void* p; p = malloc(10); p++; what happens?
p advances by sizeof(void*) bytes.
Compile-time error because pointer arithmetic on void* is not allowed.
p advances by one byte at runtime.
Runtime segmentation fault.
The C standard prohibits pointer arithmetic on void pointers since void has no size. You must cast the void* to a specific pointer type before performing arithmetic.
Given the following class definitions in C++: class A { public: void f(); }; class B { public: void f(); }; class C : public A, public B {}; What happens when you call C obj; obj.f();?
Compile-time error due to ambiguous call to f.
Calls B::f by default without ambiguity.
Runtime error because of duplicated functions.
Calls A::f by default without ambiguity.
Both A and B define f(), so obj.f() is ambiguous. The compiler cannot choose which f() to invoke, resulting in a compile-time error.
Which three member functions are referred to by the Rule of Three in C++ class design?
Constructor, destructor, equals operator.
Default constructor, destructor, move constructor.
Copy constructor, move assignment operator, destructor.
Destructor, copy constructor, copy assignment operator.
The Rule of Three states that if a class defines any of destructor, copy constructor, or copy assignment operator, it should define all three. This ensures proper resource management.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
{"name":"Which header file is required to use the printf function in C?", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"Which header file is required to use the printf function in C?, What is the output of the following C code snippet? printf(\"%d\", 10 \/ 4);, How do you correctly declare a pointer to an integer in C or C++?","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}

Learning Outcomes

  1. Analyse C and C++ code structures to identify errors
  2. Evaluate data types and understand memory allocation differences
  3. Identify appropriate control flow constructs in sample programs
  4. Apply pointers and references for dynamic memory management
  5. Demonstrate object-oriented principles in C++ class design
  6. Master usage of common standard library functions and headers

Cheat Sheet

  1. Understand Common Syntax Errors - We all forget that pesky semicolon or misplace a curly brace! Getting comfortable with slip-ups like missing semicolons, braces, and parentheses will save you heaps of debugging time. 15 Common C/C++ Mistakes
  2. Differentiate Between Data Types - Not all integers or floats are created equal; knowing their sizes and memory footprints helps avoid overflow and precision pitfalls. Mastering the nuances between char, int, float, and double keeps your code rock-solid and efficient. Data Types Deep Dive
  3. Master Control Flow Constructs - Loops and conditionals are your program's traffic lights, guiding execution from start to finish. A clear grasp of for, while, do-while, if, and switch statements turns messy code into a well-oiled machine. Control Flow Guide
  4. Utilize Pointers and References - Pointers might look scary, but they're your ticket to dynamic memory magic. Learn how to allocate, access, and free memory safely, then watch your programs soar in flexibility. Pointers & References Explained
  5. Implement Object-Oriented Principles - Classes, inheritance, polymorphism, and encapsulation are the powerhouse quartet of C++ design. Embracing these concepts lets you build modular, reusable code that's a breeze to maintain. OOP in C++
  6. Apply the Open-Closed Principle - Design your classes to welcome new features without rewriting the old code. Staying "open for extension, closed for modification" keeps your projects adaptable and bug-resistant. Open-Closed Principle
  7. Understand the Liskov Substitution Principle - If you can swap a subclass for its parent without breaking anything, you've nailed LSP. This rule ensures your inheritance hierarchies stay logical and your polymorphism plays nice. Liskov Substitution
  8. Explore Standard Library Functions - From <vector> to <algorithm>, the C++ standard library is a goldmine of ready-made tools. Learning these functions boosts your productivity and makes code cleaner. Standard Library Tips
  9. Practice Error Handling - Runtime errors can sneak up on you, but good strategies like try/catch blocks and clear error messages keep your programs sturdy. Embrace exceptions and assertions to catch bugs before they bite. Error Handling Hacks
  10. Embrace Code Reusability - Why reinvent the wheel? Use inheritance, templates, and modular design to share code across projects. Reusable components speed up development and shrink your maintenance workload. Design for Reuse
Powered by: Quiz Maker