Computer Science MCQs · C++ and Object-Oriented Programming What happens when std::map is indexed with operator[] using a key that is not present? A An exception of type std::out_of_range is thrown at once B A null reference is returned to the caller C The call fails to compile for missing keys D A default-constructed value is inserted for that key Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What is the difference between size() and capacity() on a C++ std::vector? A Both return the same value after every operation B capacity counts only elements added with push_back C size is elements stored; capacity is allocated slots Correct answer D size is allocated slots; capacity is elements stored MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming How many copies of a static data member exist for a C++ class with several objects instantiated? A One per object, created with each constructor B One per translation unit that includes the header C One per derived class that inherits it D Exactly one, shared by every object of the class Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What does the trailing const in double area() const; promise in C++? A The function returns a constant value B The function may only be called once per object C The object is copied before the call is made D The function cannot modify the object's members Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What problem does the C++ override keyword on a derived member function solve? A It allows overriding functions declared final B It makes a signature mismatch a compile-time error Correct answer C It forces the function to be inlined at call sites D It makes the base function non-virtual at run time MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming Which signature correctly declares a copy constructor for class Widget in C++? A Widget(Widget other); B Widget& operator=(Widget other); C void Widget(const Widget& other); D Widget(const Widget& other); Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming Why should a base class destructor usually be virtual when objects are deleted through base pointers? A So delete can free the object without a type check B So the derived destructor also runs during delete Correct answer C So the destructor can be called twice safely D So the base object is never actually destroyed MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming Which standard header must be included to use std::cout and std::cin in C++? A <streambuf> B <string> C <iomanip> D <iostream> Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming When is a C++ destructor automatically invoked? A Immediately after the constructor finishes B Whenever the object is passed to a function C When the object lifetime ends, such as scope exit Correct answer D Only when the programmer calls delete explicitly on a pointer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming Inside a non-static C++ member function, what is the this pointer? A A pointer to the function's own stack frame B A pointer to the object on which the member was called Correct answer C A pointer to the class's static data area D A reference to the base class subobject of the current object MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What relationship does public inheritance from a base class represent in C++? A A uses-a relationship, where the base is a local variable B A copies-a relationship, duplicating the base layout C An is-a relationship, where the derived type specialises the base Correct answer D A has-a relationship, where the base object is held as a member of the class MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What does std::vector<int>::push_back do in C++? A Inserts an element at the front of the vector B Replaces the element at the current index position C Reserves capacity without adding an element D Appends an element at the end, growing if needed Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What does the C++ expression new int(5) produce? A A reference to a temporary int value B A compile error, since new cannot take arguments C A pointer to a heap int initialised to 5 Correct answer D An int object with automatic storage duration MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What is the default access level for members of a class declared with the class keyword in C++? A private, unlike struct members which default to public Correct answer B public, the same as members of a struct C protected, so only derived classes and friends may access them D It depends on whether a constructor is declared MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming What does the compiler do when it meets a call to a function template? A It converts the arguments to a common base class B It generates a concrete function for the deduced types Correct answer C It boxes the arguments and calls a generic wrapper D It defers the call until the program is executed MCQSEXAM Show answer Read explanation
Computer Science MCQs · C++ and Object-Oriented Programming Which statement about operator overloading in C++ is correct? A The precedence of an operator can be redefined B Overloaded operators are resolved at run time C At least one operand must be a user-defined type Correct answer D Any built-in operator may be given a fresh meaning MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming After int x = (1, 2, 3); what value does x hold in C? A 6, because the values are summed B A compile-time error, since commas are invalid C 3, because the comma yields the last value Correct answer D 1, because the first value is assigned MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming For an unsigned integer x, what does the expression x & (x - 1) do? A Sets the lowest clear bit of x B Doubles the value of x C Reverses the bits of x D Clears the lowest set bit of x Correct answer MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming What is printed by printf("%d", printf("abc")); in C? A 3abc, because the inner call's value is buffered first B abc, with the outer %d printing garbage C abc3, because printf returns the characters written Correct answer D abc0, because the inner call returns nothing MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming Inside a for loop, what does the continue statement do? A Restarts the loop from its initial value B Skips the update expression for that pass only C Skips the rest of the body and runs the update step Correct answer D Exits the loop immediately and continues with the next statement MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming In C, what does sizeof('a') evaluate to on a platform where int is 4 bytes? A It is a compile-time error in every C compiler B 4, because character constants have type int Correct answer C 1, because a character occupies one byte D 2, because the value fits in a short MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming Which declaration creates a pointer to a function taking two ints and returning an int? A int *(fp(int, int)); B int fp(*)(int, int); C int (*fp)(int, int); Correct answer D int *fp(int, int); MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming What does fopen("data.txt", "w") do when data.txt already exists? A Opens it read-only until data is written B Truncates the file to zero length before writing Correct answer C Appends new text after the existing content of the file D Fails and returns NULL to protect the file MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming If int *p holds address 1000 and int is 4 bytes, what does p++ leave in p? A 1004, only if p points to an array B 1004, one int further along in memory Correct answer C 1001, one byte further along in memory D 1000, since the address is unchanged MCQSEXAM Show answer Read explanation
Computer Science MCQs · C Programming On a typical 64-bit system with 4-byte int, what does sizeof return for struct S { char c; int i; };? A 8, because padding aligns the int to 4 bytes Correct answer B 5, the sum of the two member sizes C 4, because the char is packed inside the int D 16, because every struct is 16-byte aligned MCQSEXAM Show answer Read explanation