Introduction To C Plus Plus - Study Mode
[#126] What happens if the following line is executed in C and C++? const int a
Correct Answer
(D) Error in C++ and successful execution in C
[#127] What happens if we run the following code in both C and C++? #include<stdio.h>
struct STRUCT
{
int a = 5
int func()
{
printf("%d
", a)
}
}
int main()
{
struct STRUCT s
s.func()
return 0
}
Correct Answer
(B) The program gives an error in case of C but runs perfectly in case of C++
[#128] What will be the output of the following C++ code? #include<iostream>
using namespace std
class A
{
~A(){
cout<<"Destructor called
"
}
}
int main()
{
A *a1 = new A()
A *a2 = new A()
return 0
}
Correct Answer
(D) Nothing is printed
[#129] What is the output of the following code: cout << (5 > 2) ? "True" : "False"
Correct Answer
(A) TRUE
Explanation
Solution: The given code uses the ternary operator to evaluate a condition and print a result based on that evaluation. The code is: cout << (5 > 2) ? "True" : "False"
The expression (5 > 2) evaluates to true because 5 is greater than 2. When the condition is true , the ternary operator returns the first value after the question mark (?). In this case, it returns "True" . Therefore, the correct output of the code is True . Correct Answer: Option A: TRUE
[#130] What will be the output of the following C++ code? #include <iostream>
class Test
{
public:
void fun()
}
static void Test::fun()
{
std::cout<<"fun() is static"
}
int main()
{
Test::fun()
return 0
}
Correct Answer
(B) Compile-time Error