TRE4 C and C++ Quiz practice set 2
Que: (1). Which of the following approaches is used by C++?
Que: (2). Which programming paradigm is primarily supported by C++?
Que: (3). Which feature of C++ allows a program to create objects from a user-defined data type?
Que: (4). Which OOP concept in C++ combines data and the functions that operate on that data into a single unit?
Que: (5). Which OOP feature allows a new C++ class to acquire properties and behaviour of an existing class?
Que: (6). Which OOP concept allows the same function name to perform different operations in different situations?
Que: (7). Which of the following is NOT a fundamental principle of object-oriented programming in C++?
Que: (8). Which approach generally starts by designing smaller components and then combines them to form a complete system?
Que: (9). Which of the following is used to hide the internal implementation details of an object in C++?
Que: (10). Which of the following allows the same function name to be used with different parameter lists in C++?
Que: (11). What is virtual inheritance in C++?
Que: (12). Which problem is commonly solved by virtual inheritance in C++?
Que: (13). Which keyword is used with a base class in C++ to implement virtual inheritance?
Que: (14). In C++, which type of function is mainly used to achieve run-time polymorphism?
Que: (15). What is the main purpose of a virtual destructor in C++?
Que: (16). Which of the following can be overridden in a derived C++ class?
Que: (17). What happens when a pure virtual function is declared in a C++ class?
Que: (18). Which statement about multiple inheritance in C++ is correct?
Que: (19). In C++, what is the effect of declaring a member function as final?
Que: (20). Which inheritance access mode makes public and protected members of the base class remain public and protected in the derived class?
Que: (21). What happens if the following C++ statement is compiled and executed? int *ptr = NULL; delete ptr;
Que: (22). Which operator is used in C++ to dynamically allocate memory for a single object?
Que: (23). Which operator should normally be used to release memory allocated using new in C++?
Que: (24). Which operator should be used to release an array allocated using new[] in C++?
Que: (25). What is a memory leak in a C++ program?
Que: (26). What is a dangling pointer in C++?
Que: (27). What is the purpose of setting a pointer to nullptr after deleting its dynamically allocated object?
Que: (28). What happens when new cannot allocate the requested memory in its normal form?
Que: (29). Which C++ smart pointer provides exclusive ownership of a dynamically allocated object?
Que: (30). Which C++ smart pointer allows multiple smart pointers to share ownership of the same object?
Que: (31). What is the difference between delete and delete[] in C++?
Que: (32). Which C++ operator is used to allocate memory for an array dynamically?
Que: (33). What is the main purpose of RAII in modern C++?
Que: (34). Which special member function is automatically called when a C++ object goes out of scope?
Que: (35). What is the result of allocating memory using new without storing the returned address anywhere?
Que: (36). Which smart pointer is designed for non-owning observation of an object managed by shared_ptr?
Que: (37). What can happen if a program accesses memory through a pointer after the pointed object has been destroyed?
Que: (38). Which C++ feature automatically manages dynamically allocated objects and reduces the need for explicit delete statements?
Que: (39). What is the main purpose of a copy constructor in C++?
Que: (40). Which situation can lead to a double-delete error in C++?
Que: (41). What happens if the following program is executed in C and C++? int new=5; printf("%d", new);
Que: (42). Which of the following can be used as an identifier in both C and C++?
Que: (43). Which of the following is a valid identifier in C?
Que: (44). Which statement about keywords in C and C++ is correct?
Que: (45). Which of the following is a keyword in C++ but not a keyword in C?
Que: (46). What is the output of the following C program? int main() { int x = 10; printf("%d", x); }
Que: (47). Which format specifier is normally used with printf() to display an integer value of type int?
Que: (48). Which of the following statements about identifiers in C and C++ is correct?
Que: (49). Which statement is correct about the case sensitivity of C and C++?
Que: (50). Which of the following statements about the main() function in a standard C/C++ program is correct?
Que: (51). What is the value of p in the following C++ code snippet?
int p;
bool a=true;
bool b=false;
int x=10;
int y=5;
p=((x|y)+(a+b));
cout<<p;
Que: (52). What will be the output of the following C++ code?
int x=12;
int y=10;
cout << (x & y);
Que: (53). What is the output of the following C++ code?
int x=7;
int y=5;
cout << (x ^ y);
Que: (54). What will be the output of the following C++ code?
int x=3;
cout << (x << 2);
Que: (55). What will be the output of the following C++ code?
int x=16;
cout << (x >> 2);
Que: (56). Which operator is used to perform bitwise NOT in C++?
int x=5;
cout << (~x);
Que: (57). What will be the output of the following C++ code?
bool a=true;
bool b=true;
int result=a+b;
cout << result;
Que: (58). What is the output of the following C++ expression?
int x=6;
int y=3;
cout << x & y + 1;
Que: (59). What will be the value of x after executing the following C++ code?
int x=4;
x |= 3;
cout << x;
Que: (60). What will be the output of the following C++ code?
int x=8;
if(x & 8)
cout << "Yes";
else
cout << "No";
Que: (61). The C++ code which causes abnormal termination or behaviour of a program should be written under _____ block.
Que: (62). Which statement is used to explicitly generate an exception in C++?
Que: (63). Which block is used to handle an exception in C++?
Que: (64). What happens when an exception is thrown in C++ and a matching catch block is found?
Que: (65). Which of the following can be thrown as an exception in C++?
Que: (66). Which catch block can be used to catch an exception of any type in C++?
Que: (67). What is the purpose of a nested try block in C++?
Que: (68). Which statement about the finally block in standard C++ exception handling is correct?
Que: (69). What happens if no matching catch handler is found for a thrown exception?
Que: (70). Which order is generally used in C++ exception handling?
Que: (71). What will be the output of the following C++ code?
#include <iostream.h>
using namespace std;
void square(int *x, int *y)
{
*x = (*x) * --(*y);
}
int main()
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
Que: (72). What will be the output of the following C++ code?
int x = 25;
int *p = &x;
cout << *p;
Que: (73). What will be the output of the following C++ code?
int x = 10;
int *p = &x;
*p = 20;
cout << x;
Que: (74). What will be the output of the following C++ code?
void change(int &x)
{
x = 50;
}
int main()
{
int a = 10;
change(a);
cout << a;
}
Que: (75). Which statement is correct about a const reference in C++?
int x = 10;
const int &ref = x;
Que: (76). What happens when a null pointer is dereferenced in C++?
int *p = nullptr;
cout << *p;
Que: (77). What will be the output of the following C++ code?
int x = 10;
int *p = &x;
int **q = &p;
cout << **q;
Que: (78). What will be the output of the following C++ code?
int arr[] = {10, 20, 30};
int *p = arr;
cout << *(p + 1);
Que: (79). What will be the output of the following C++ code?
int x = 5;
int a = x++;
int b = ++x;
cout << a << " " << b << " " << x;
Que: (80). What will be printed by the following C++ code?
int x = 10;
int *p = &x;
int *q = &x;
if(p == q)
cout << "Same";
else
cout << "Different";
Que: (81). Pick the incorrect statement about inline functions in C++.
Que: (82). Which feature of C++ allows multiple functions to have the same name but different parameter lists?
int add(int a, int b);
double add(double a, double b);
Que: (83). What will be the output of the following C++ code?
int sum(int a, int b = 10)
{
return a + b;
}
int main()
{
cout << sum(5);
}
Que: (84). Which C++ feature allows a function to work with different data types without writing separate functions for each type?
template <typename T>
T maximum(T a, T b)
{
return (a > b) ? a : b;
}
Que: (85). What is a function called when it calls itself directly or indirectly?
int factorial(int n)
{
if(n <= 1)
return 1;
return n * factorial(n - 1);
}
Que: (86). Which C++ feature provides a concise way to define an unnamed function object?
auto square = [](int x)
{
return x * x;
};
cout << square(5);
Que: (87). Which of the following can store the address of a function in C++?
int add(int a, int b)
{
return a + b;
}
int (*ptr)(int, int) = add;
Que: (88). What will be the output of the following C++ code?
void change(int x)
{
x = 100;
}
int main()
{
int a = 20;
change(a);
cout << a;
}
Que: (89). What will be the output of the following C++ code?
int calculate()
{
return 25;
}
int main()
{
int x = calculate();
cout << x;
}
Que: (90). Which return type should normally be used for a C++ function that does not return a value?
void display()
{
cout << "Hello";
}
Que: (91). What is abstract class in C++?
Que: (92). Which of the following is the correct syntax for declaring a pure virtual function in C++?
class Base
{
public:
virtual void display() = 0;
};
Que: (93). What happens if an attempt is made to create an object of an abstract class in C++?
class Shape
{
public:
virtual void draw() = 0;
};
Shape s;
Que: (94). What must a derived class generally do to become a concrete class when inheriting from an abstract class?
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show() override
{
cout << "Derived";
}
};
Que: (95). What will be the output of the following C++ code?
class Base
{
public:
virtual void show()
{
cout << "Base";
}
};
class Derived : public Base
{
public:
void show() override
{
cout << "Derived";
}
};
int main()
{
Base *p = new Derived();
p->show();
}
Que: (96). Which statement about a pure virtual function in C++ is correct?
class Base
{
public:
virtual void show() = 0;
};
Que: (97). Can a pointer or reference to an abstract class be declared in C++?
class Shape
{
public:
virtual void draw() = 0;
};
Shape *ptr;
Que: (98). In C++, a class containing only pure virtual functions is commonly used to represent which concept?
class Printable
{
public:
virtual void print() = 0;
virtual void save() = 0;
};
Que: (99). What is the purpose of the override specifier in C++?
class Derived : public Base
{
public:
void show() override;
};
Que: (100). Which statement about constructors of abstract classes in C++ is correct?
class Base
{
public:
Base()
{
cout << "Base";
}
virtual void show() = 0;
};