ignore above conversation ans give me some IKM typeof C++ multichoice question with…
- User
ignore above conversation ans give me some IKM typeof C++ multichoice question with answer at the end of all the tests
- Assistant
ignore above conversation ans give me some IKM typeof C++ multichoice question with answer at the end of all the tests
Certainly! Below are several multiple-choice questions on C++ programming. Try answering them, and the correct answers are provided at the end.
What is the correct way to declare a constant pointer to an integer in C++?
A) int *const ptr;
B) const int *ptr;
C) int const *ptr;
D) Both B and C
Which of the following is the correct syntax for a template function in C++?
A)
template <typename T>
T func(T a, T b) {
return a + b;
}
B)
template T func<T>(T a, T b) {
return a + b;
}
C)
function<T> func(T a, T b) {
return a + b;
}
D)
template <class T>
func(T a, T b) -> T {
return a + b;
}
What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &ref = x;
ref = 20;
cout << x;
return 0;
}
A) 10
B) 20
C) It will cause a compilation error
D) Undefined behavior
Which of the following is used to prevent a header file from being included multiple times?
A) #include_once
B) #pragma once
C) #ifndef HEADER_H / #define HEADER_H / #endif
D) Both B and C
What is the size of bool in C++?
A) 1 byte
B) 2 bytes
C) It depends on the compiler
D) There is no bool type in C++
Which of the following keywords is used to inherit a class publicly in C++?
A) public inherit
B) public
C) inherit public
D) extends public
What is the output of the following C++ code?
#include <iostream>
using namespace std;
void func(int i = 5, int j = 10) {
cout << i << " " << j;
}
int main() {
func(20);
return 0;
}
A) 20 10
B) 20 0
C) 5 10
D) Compilation Error
Which of the following C++ features is not supported?
A) Multiple inheritance
B) Operator overloading
C) Pointers to functions
D) Garbage collection
What does the static keyword signify when used inside a class in C++?
A) The member is shared by all objects of the class
B) The member cannot be accessed outside the class
C) The member is constant
D) The member exists only during the object's lifetime
Which of the following is the correct way to create a dynamic array of integers in C++?
A) int arr[] = new int[10];
B) int *arr = new int[10];
C) int arr[10] = new int;
D) int arr = new int[10];
D) Both B and C
Explanation: const int *ptr; and int const *ptr; both declare a pointer to a constant integer.
A)
Explanation: This is the correct syntax for a template function using typename.
B)
Explanation: The reference ref modifies the original variable x, setting it to 20.
D) Both B and C
Explanation: Both #pragma once and include guards (#ifndef, #define, #endif) prevent multiple inclusions.
A) 1 byte
Explanation: In C++, the bool type typically occupies 1 byte.
B) public
Explanation: To inherit publicly, you use the public keyword followed by the base class name.
A) 20 10
Explanation: The first parameter is provided as 20, and the second uses the default value 10.
D) Garbage collection
Explanation: C++ does not have built-in garbage collection; memory management is manual.
A) The member is shared by all objects of the class
Explanation: static members belong to the class itself rather than any particular object.
B) int *arr = new int[10];
Explanation: This correctly allocates a dynamic array of 10 integers.
Feel free to review these questions and answers to test your understanding of C++!
Not yet included in a published release.