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

Technical Interview Quiz: C/C++ Programming Challenge

Test Your C and C++ Skills Today

Difficulty: Moderate
Questions: 20
Learning OutcomesStudy Material
Colorful paper art depicting a technical interview quiz on CC programming.

Looking to ace your technical interview in C/C++ programming? This practice quiz covers pointers, memory management, and templates to sharpen your coding skills. It's perfect for students and professionals prepping for real-world coding challenges. Customize it freely in our editor to focus on areas you need most. Explore related C Programming Fundamentals Quiz or advance with our Software Engineer Technical Interview Quiz and browse other quizzes.

Which statement correctly declares a null pointer to int in C++?
int* p = 0;
int* p = NULL;
int* p = nullptr;
int* p;
The nullptr keyword, introduced in C++11, is the type-safe null pointer literal. Using nullptr avoids ambiguities associated with 0 or NULL.
Given the code 'int a = 5; int* p = &a; std::cout << *p;', what is printed?
5
The memory address of a
Compilation error
A garbage value
*p dereferences the pointer p, yielding the value of a, which is 5. No errors occur since p holds a valid address.
Which STL container implements FIFO (first-in, first-out) behavior?
std::stack
std::queue
std::priority_queue
std::vector
std::queue is designed for FIFO operations, where elements are added at the back and removed from the front. std::stack provides LIFO behavior instead.
Which operator must be used to free memory allocated with new?
free()
delete
delete[]
remove()
Memory allocated with new must be freed with delete. Using delete[] is for arrays allocated with new[]. free() is for malloc, not new.
How do you declare a function template in C++?
template void func(T t)
template<> void func(int t)
void func(T t)
template void func(int)
The syntax template void func(T t) defines a generic function template parameterized by type T. The other forms are either specializations or invalid.
Given 'int arr[] = {1,2,3,4}; int* p = arr + 2; std::cout << *(p - 1);', what is printed?
1
2
3
4
arr+2 points to the element arr[2] which is 3. Subtracting one gives a pointer to arr[1], whose value is 2. Thus *(p-1) prints 2.
Which code snippet causes a memory leak?
int* p = new int(5);
int* p = new int; delete p;
int* p = (int*)malloc(sizeof(int)); free(p);
int* p = new int[10]; delete p;
The snippet int* p = new int(5); never calls delete, so the allocated memory is never freed. The other examples properly free allocated memory or misuse delete syntax but still free memory.
What is needed to perform a deep copy of a class managing a dynamic int array?
A custom copy constructor that allocates new storage and copies elements
The default copy constructor
A custom assignment operator only
Only a user-defined destructor
A deep copy requires a custom copy constructor that allocates its own memory and copies each element. The default copy constructor only copies the pointer, causing shared ownership.
Which syntax declares a full specialization of the template 'template void f(T)' for int?
template<> void f(int)
template void f(int)
template<> void f(int)
template void f(int)
Full specialization uses template<> and specifies the type in angle brackets: template<> void f(int). Other forms are either partial or invalid.
What is a common cause of a segmentation fault in C++?
Dereferencing a null or invalid pointer
Using a reference variable
Dereferencing a valid pointer
Overflowing an unsigned integer
Dereferencing a null or otherwise invalid pointer accesses memory the program doesn't own, causing a segmentation fault. References and unsigned overflow do not cause SIGSEGV directly.
What is the time complexity of inserting an element at the beginning of a std::vector of size n?
O(1)
O(n)
O(log n)
O(n^2)
Inserting at the beginning of a std::vector shifts all existing elements one position, resulting in linear time complexity O(n).
How do you declare a pointer to a function that takes a double and returns an int?
int* f(double);
int (*f)(double);
int (f*)(double);
int (*f)(double, int);
The correct syntax for a function pointer taking a double and returning an int is int (*f)(double). Other forms are either function declarations or mismatched signatures.
Which code correctly captures all external variables by reference in a lambda expression?
[=](){ /* ... */ }
[&](){ /* ... */ }
[this](){ /* ... */ }
[](){ /* ... */ }
Using [&] in the lambda capture list makes all referenced external variables captured by reference, allowing modification. [=] captures by value, [] captures none.
How do you declare an rvalue reference to int in C++11?
int&
int&&
int*&&
int&&&
In C++11, the syntax int&& declares an rvalue reference to int. A single ampersand & is an lvalue reference, and &&& is invalid.
What is a key advantage of std::unique_ptr over raw pointers?
Automatic memory deallocation when the pointer goes out of scope
Faster runtime performance
Ability to be copied freely
Consumes less memory
std::unique_ptr automatically deletes its managed object when it goes out of scope, preventing leaks. It is move-only, so it cannot be copied freely.
In a diamond inheritance scenario, which keyword ensures only one instance of the common base is used?
public
virtual public
protected
private
Virtual inheritance (virtual public) ensures that only one shared base class subobject is present in the diamond hierarchy. Other inheritance forms create multiple base instances.
Which template construct uses SFINAE to enable a function only for integral types?
template::value>> void f(T)
template void f(T) requires std::is_integral::value
template void f(T) if(std::is_integral::value)
template typename std::enable_if::value>::type f(T)
Using a default template parameter with std::enable_if_t and SFINAE disables the template for non-integral types at compile time. The requires clause is C++20, not SFINAE.
Which scenario triggers undefined behavior in C++?
Signed integer overflow
Unsigned integer overflow
Shifting bits within range
Comparing pointers within the same array
Signed integer overflow is undefined behavior in C++. Unsigned overflow wraps around, and pointer comparisons or valid bit shifts are well-defined.
Why might you implement a custom allocator for std::vector?
To optimize memory allocation patterns for specific workloads
To change element access syntax
To add built-in thread-safety
To avoid template instantiation
Custom allocators allow tuning of memory allocation strategies to reduce fragmentation or improve performance. They do not alter syntax or thread safety inherently.
Which C++11 feature is fundamental for implementing lock-free data structures?
std::mutex
std::atomic operations
std::thread
std::condition_variable
std::atomic provides atomic read-modify-write operations necessary for lock-free concurrency. mutex and condition_variable involve locking mechanisms.
0
{"name":"Which statement correctly declares a null pointer to int in C++?", "url":"https://www.quiz-maker.com/QPREVIEW","txt":"Which statement correctly declares a null pointer to int in C++?, Given the code 'int a = 5; int* p = &a; std::cout << *p;', what is printed?, Which STL container implements FIFO (first-in, first-out) behavior?","img":"https://www.quiz-maker.com/3012/images/ogquiz.png"}

Learning Outcomes

  1. Analyze pointer and memory management concepts
  2. Evaluate data structures implementations in C/C++
  3. Master template and generic programming techniques
  4. Identify common syntax and logic pitfalls
  5. Demonstrate proficiency with algorithmic code challenges
  6. Apply effective debugging and optimization strategies

Cheat Sheet

  1. Master Smart Pointers for Safe Memory Management - Protect your code from messy manual memory handling by using std::unique_ptr and std::shared_ptr. These smart pointers automatically free memory when no longer needed, so you can kiss leaks and dangling pointers goodbye! Smart Pointers Deep Dive
  2. Implement RAII for Resource Safety - Treat every resource like a VIP guest: acquire it in a constructor and release it in a destructor. The Resource Acquisition Is Initialization pattern ensures cleanups happen even when code throws exceptions, making your programs far more robust. RAII Explained
  3. Choose Appropriate Data Structures for Efficiency - Pick the right container for the job - use std::vector for fast random access or std::unordered_map for quick lookups. Avoid using linked lists when you need speedy indexing, since they're much slower on modern CPU caches. Data Structures Guide
  4. Utilize Move Semantics to Avoid Unnecessary Copies - Think of move semantics as a teleportation device that zaps resources from one object to another without the heavy lifting of a full copy. By using std::move, you can supercharge performance when handling large objects like big data buffers. Move Semantics Tips
  5. Be Vigilant About Common Memory Errors - Stay on the lookout for memory leaks, dangling pointers, and buffer overflows that can crash your program or cause weird bugs. Regularly run code reviews and integrate static analysis tools to catch these gremlins before they escape into production. Memory Error Prevention
  6. Optimize Code with Profiling Tools - Don't guess where your bottlenecks hide - profile your code with tools that pinpoint slow spots. By targeting optimizations where they matter most, you'll spend less time micro-tweaking and more time building cool features. Profiling Best Practices
  7. Ensure Proper Memory Alignment - Align your data structures correctly to avoid nasty crashes on some architectures and to squeeze out extra performance. Use the alignas specifier when putting together performance-critical structs or buffers. Memory Alignment Basics
  8. Leverage Debugging Tools for Memory Issues - Arm yourself with Valgrind and AddressSanitizer to hunt down leaks, out-of-bounds accesses, and undefined behavior. These powerful tools are like virtual detectives, giving you clear clues so you can fix bugs faster. Valgrind & AddressSanitizer
  9. Understand and Apply Move Semantics Deeply - Go beyond basic moves by learning how rvalue references and perfect forwarding can make your template code both safe and lightning-fast. Mastering these concepts will turn you into a performance ninja when dealing with temporary objects. Move Semantics Mastery
  10. Practice Effective Debugging Techniques - Build strong habits like setting strategic breakpoints, inspecting variable states, and sprinkling in logging statements to trace tricky logic. Sharpening these skills will help you squash bugs quickly and keep your codebase shining. Debugging Techniques
Powered by: Quiz Maker