C++ Questions and Answers Part-7

1. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin: " << cin;
return 0;
}
a) cin: garbage value
b) Error
c) Segmentation fault
d) Nothing is printed

Answer: a
Explanation: cin is a variable hence overrides the cin object. cin >> cin has no meaning so no error

2. Which of the following operator has left to right associativity?
a) Unary operator
b) Logical not
c) Array element access
d) addressof

Answer: c
Explanation: Array element has left to right associativity i.e. expressions are evaluated from left to right in case of array element access.

3. Which of the following is accessed by a member function of a class?
a) The object of that class
b) All members of a class
c) The public part of a class
d) The private part of a class

Answer: b
Explanation: A member function of a class can access all the members of its class whether they are private, protected or public.

4. What is the size of a character literal in C and C++?
a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4

Answer: a
Explanation: The size of a character literal is 4 in case of C but it is one in case of C++. You can do printf(“%d”, (int)sizeof(‘a’)); in both C and C++ to check this.

5. What is the size of a character type in C and C++?
a) 4 and 1
b) 1 and 4
c) 1 and 1
d) 4 and 4

Answer: c
Explanation: The size of a character type in both C and C++ is 1. You can do printf(“%d”, (int)sizeof(char)); in both C and C++ to check this.

6. Which of the following is correct?
a) struct tag is required in both C and C++ while declaring an object of the structure
b) struct is not required in C but required in C++ while declaring an object of the structure
c) struct is not required in C++ but required in C while declaring an object of the structure
d) struct tag is not required in both C and C++ while declaring an object of the structure

Answer: c
Explanation: C++ does not require struct keyword while declaring an object of the structure whereas in C we require struct tag for declaring an object.

7.Which of the following is correct?
a) struct cannot have member function in C but it can in C++
b) struct cannot have member function in C++ but it can in C
c) struct cannot have member function in both C and C++
d) struct can have member function in both C and C++

Answer: a
Explanation: struct can have member function in C++ whereas member functions are not allowed in case of C.

8. What happens if we run the following code in both C and C++?
#include <stdio.h>
struct STRUCT
{
int a;
int func()
{
printf("HELLO THIS IS STRUCTURE\n");
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++

Answer: b
Explanation: As C does not allows the structure to have member functions, therefore, it gives an error in case of C but as C++ does allow structures to have member functions, therefore, the C++ does not give an error.

9. 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\n", a);
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++

Answer: b
Explanation: As C does not allows to initialize any member inside the structure, therefore, the program gives error whereas in case of C++ this is allowed therefore the program does not give any error.

10. What happens if the following program is compiled in both C and C++?
#include <stdio.h>
struct STRUCT
{
int static a;
};
int main()
{
struct STRUCT s;
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++

Answer: b
Explanation: C does not allow the programmer to declare any static members inside a class whether in C++ it is allowed to declare static variables.