C++ Programming Questions and Answers – C++ Concepts – 2

Q 1. Which of the following is the scope resolution operator?
A. .
B. *
C. ::
D. ~

Show Answer Answer:-C. ::
Explanation :: operator is called scope resolution operator used for accessing a global variable from a function which is having the same name as the variable declared in the function.

Q 2. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int x = 1;
int main()
{
    int x = 2;
    {
        int x = 3;
        cout << ::x << endl;
    }
    return 0;
}

A. 1
B. 2
C. 3
D. 123

Show Answer Answer:-A. 1
Explanation While printing x we are using :: operator hence the refernce is given to global variable hence the global variable x = 1 is printed.

Q 3. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class A
{
  ~A(){
    cout<<"Destructor called\n";
  }
};
int main()
{
    A a;
    return 0;
}

A. Destructor called
B. Nothing will be printed
C. Error
D. Segmentation fault

Show Answer Answer:-C. Error
Explanation Whenever a destructor is private then one should not define any normal object as it will be destroyed at the end of the program which will call destructor and as destructor is private the program gives error during compile while in case of pointer object the compiler at compile does not know about the object, therefore, does not gives compile error. Hence when the destructor is private then the programmer can declare pointer object but cannot declare a normal object.

Q 4. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class A
{
  ~A(){
     cout<<"Destructor called\n";
   }
};
int main()
{
    A *a1 = new A();
    A *a2 = new A();
    return 0;
}

A. Destructor called
B. Destructor called

C. Error
D. Nothing is printed

Show Answer Answer:-D. Nothing is printed
Explanation The pointer object is created is not deleted hence the destructor for these objects is not called hence nothing is printed on the screen.

Q 5. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
int x[100];
int main()
{
    cout << x[99] << endl;
}

A. Garbage value
B. 0
C. 99
D. Error

Show Answer Answer:-B. 0
Explanation In C++ all the uninitialized variables are set to 0 therefore the value of all elements of the array is set to 0.

Q 6. 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. Error cin:
B. garbage value
C. Segmentation fault
D. Nothing is printed
View Answer

Show Answer Answer:-B. garbage value
Explanation cin is a variable hence overrides the cin object. cin >> cin has no meaning so no error.

Q 7. Which of the following operator has left to right associativity?
A. Unary operator
B. Logical not
C. Array element access
D. address of

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

Q 8. Which of the following is accessed by a member function of a class?
A. The object of that class
B. The private part of a class
C. The public part of a class
D. All members of a class

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

Q 9. What is the size of a character literal in C and C++?
A. 1 and 1
B. 1 and 4
C. 4 and 1
D. 4 and 4

Show Answer Answer:-C. 4 and 1
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.

Q 10. 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

Show Answer Answer:-C. 1 and 1
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.

Q 11. 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

Show Answer Answer:-C. struct is not required in C++ but required in C while declaring an object of the structure
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.

Q 12. 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++

Show Answer Answer:-A. struct cannot have member function in C but it can in C++
Explanation struct can have member function in C++ whereas member functions are not allowed in case of C.

Q 13. 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++

Show Answer Answer:-B. The program gives an error in case of C but runs perfectly in case of C++
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.

Q 14. 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++

Show Answer Answer:-B. The program gives an error in case of C but runs perfectly in case of C++
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.

Q 15. 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 both C and 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 C but runs perfectly in case of C++

Show Answer Answer:-D. The program gives an error in case of C but runs perfectly in case of C++
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page