Alien head

C++ Questions

Description: Questions which can be used for B.E(First Year), BCA, MCA and IT Placement tests
Number of Questions: 14
Created by:
Tags: c c plus plus Java/Oracle /C/C++
Attempted 0/14 Correct 0 Score 0

What does a compiler do?

  1. It translates source code into activities (which may comprise groups of machine instructions) and immediately thereafter executes those activities.

  2. It translates source code directly into assembly language or machine instructions. The eventual end product is a file or files containing machine code.

  3. It combines a list of object modules into an executable program that can be loaded and run by the operating system.

  4. It only enables the programmer to edit the program files.


Correct Option: B
Explanation:

This job is performed by compiler.

How does the do-while loop different from the while loop?

  1. The do-while loop is faster than the while loop.

  2. The do-while loop is slower than the while loop.

  3. There is no difference.

  4. In a do-while loop, the statement always executes at least once.


Correct Option: D
Explanation:

In a do-while loop, the condition comes after the statement. So, the statement always executes at least once.

What does the following C++ statement do?
extern int a;

  1. Declaration of variable 'a' without definition

  2. Definition of variable 'a'

  3. Declaration and definition of variable 'a'

  4. Declaration of function pointer 'a'


Correct Option: A
Explanation:

Extern is used for declaring and not defining.

If a class declares a constructor taking one or more parameters but does not declare a default constructor, then

  1. every class object definition must provide the required arguments

  2. it is a compilation error

  3. it is a runtime error

  4. the compiler automatically inserts the default constructor


Correct Option: A
Explanation:

The programmer has to follow the constructor signature.

Assume all the relevant header files are included.

In the following code, which function will be called?
void print(int) {}
void print(int&) {}
int iobj;
int &ri = iobj;

int main() {

print(iobj);

}

  1. Print(int)

  2. Print(int&)

  3. Compilation error: Ambiguous

  4. Runtime error


Correct Option: C
Explanation:

Both the functions can be called. So, the compiler flags an error.

A private member can be accessed only by the

  1. member functions

  2. member functions and friends of its class

  3. member functions and derived class member functions

  4. static functions


Correct Option: B
Explanation:

Yes, it is correct for class access specifiers.

Consider the following array definition
int ia[] = { 0, 1, 2, 3, 4, 5 };

What is the equivalent of the following statement?
ia;

  1. ia[0];

  2. *ia;

  3. &ia[0];

  4. Ia[1];


Correct Option: C
Explanation:

It gives the address of the first element of the array.

Which of the following STL containers is an appropriate choice in a situation where a random access into a container is required?

  1. Vector

  2. List

  3. Both list and vector

  4. Random access is not supported in STL.


Correct Option: A
Explanation:

Vector consists of contiguous memory location and is the appropriate choice.

Which of the following is NOT true for C++ reference?

  1. A reference must be initialised, when it is created.

  2. Once a reference is initialized to an object, it cannot be changed to refer to another object.

  3. NULL references are not allowed.

  4. When a reference is used as a function argument, any modification to the reference inside the function will not cause changes to the argument outside the function.


Correct Option: D
Explanation:

It is NOT a property of the C++ reference. It shows incorrect understanding of C++ reference concepts.

What is the output of the following program?
Assume sizeof(int) = 4

#include <iostream>
using namespace std;

class Base {
static int b;
};
class Derived : public Base {
int d;
};
int main() {
Derived objD;
cout << sizeof(objD) << endl;
}

  1. 4

  2. 8

  3. 5

  4. 12


Correct Option: A
Explanation:

Static variables are not inherited. Therefore, sizeof(objD) = sizeof(d)

What is the output of the following program?

#include <iostream>
using namespace std;

class Base {
public:
virtual void fun() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
void fun() { cout << “Derived Class” << endl; }
};
int main() {
Derived objD;
Base *ptrB = &objD;
ptrB->fun();
}

  1. Base Class

  2. Derived Class

  3. No output. A Compilation Error

  4. No output. A Runtime Error


Correct Option: B
Explanation:

Runtime binding of a virtual function based on the object the pointer is pointing to. This is called Runtime Type Identification (RTTI).

What is the output of the following program?

#include <iostream>
using namespace std;

class Base {
public:
Base() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
Derived() { cout << “Derived Class” << endl; }
};
int main() {
Derived objD;
}

  1. Derived Class

  2. Base Class

  3. Derived Class
    Base Class

  4. Base Class
    Derived Class


Correct Option: D
Explanation:

Base class constructor is called before derived class.

What is the output of the following program?

#include <iostream>
using namespace std;

class Base {
public:
void fun() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
void fun() { cout << “Derived Class” << endl; }
};
int main() {
Derived objD;
Base *ptrB = &objD;
ptrB->fun();
}

  1. Base Class

  2. Derived Class

  3. No output. A Compilation Error

  4. No output. A Runtime Error


Correct Option: A
Explanation:

Compile time binding of a non-virtual function is based on the data type of the pointer.

What is the output of the following program?

Assume sizeof( int ) = 4

#include <iostream>
using namespace std;

class Base {
public:
virtual void fun() { cout << “Base Class” << endl; }
};
class Derived : public Base {
public:
void fun() { cout << “Derived Class” << endl; }
};
int main() {
Base objBase;
cout << sizeof( objBase ) << endl;
}

  1. 0

  2. 1

  3. 4

  4. 8


Correct Option: C
Explanation:

An invisible virtual pointer is automatically inserted in a class when at least one of its methods is declared as virtual. The size of a pointer is 4 bytes.

+ View questions