0

C++ Programming

Description: This test focuses on fundamental & programming skill concepts in C++ programming
Number of Questions: 15
Created by:
Tags: Cpp Programming in C and C++ Java/Oracle /C/C++
Attempted 0/15 Correct 0 Score 0

What is the output of C++ program given below?

#include<iostream> using namespace std; int main() { typedef int hello; hello i=9.9; cout<<i; }

  1. 9

  2. 9.9

  3. 9.0

  4. Error in the program

  5. None of the above


Correct Option: A
Explanation:

 typedef creates a new name for a given datatype.

typedef int hello;     //hello is the new name of integer datatype.

hello i=9.9; cout<<i;

Which displays value of 'i'. As 'i' is of integer datatype because is declared as hello type which is the name given to integer datatype using typedef. Hence, prints 9.

 

What is the output of C++ program given below?

#include<iostream> using namespace std; int main() { int y=8,x=6; (x=5)&&(x=7) ? y=1:y=2; cout<<x<<"_"<<y; }

Note: _ (underscore is used in cout statement to differentiate x & y value.

  1. 1_7

  2. 7_1

  3. 5_1

  4. 6_2

  5. None of the above


Correct Option: B
Explanation:

 Though variables x,y are intialized with 6, 8 respectively. Due to below statement the values of x,y are changed. (x=5)&&(x=7) ? y=1:y=2; Conditional operator (?:) follows right to left associativity. && operator follows left to right associativity. = opeartor always returns true. Evaluate the condition (x=5)&&(x=7) first. proceed left to right due to && Hence x=7 finally, and condition returns true because (true&&true implies true) Hence y=1; Which prints 7_1

Which of the following statements is false regarding to namespace in C++ programming?

  1. C++ provides declaring an alias name for a given namespace.

  2. Namespace can be declared without any name or even unnamed.

  3. Namespace defination can be extended over multiple files.

  4. Instance variable can be created for a namespace.

  5. Namespace defination should not be terminated by a semicolon.


Correct Option: D
Explanation:

 We cant create instance variable's for namespace. There are not similar to classes in C++.

This option is false statement. Hence this option is the correct choice to be choosen as a false statement.

What is the output of C++ program given below?

#include<iostream> using namespace std; int main() { int i,j,k=5; for(i=1;i<=5;i++) { for(j=1;j<=k;j++) { cout<<j; } k=k-1; cout<<"n"; } }

  1. 12345 12345 12345 12345 12345

  2. 12345 1234 123 12 1

  3. 1 12 123 1234 12345

  4. Error in the program

  5. None of the above


Correct Option: B
Explanation:

Lets trace out i,j,k values of the given program:|k|i|j| |---|---|---| |5|1|1 2 3 4 5| |4|2|1 2 3 4| |3|3|1 2 3| |2|4|1 2| |1|5|1|

As we are displaying 'j' values, hence the output will be the similar to column 'j' as shown in the given table. In the table each row represents a single iteration of first for loop. We are decrementing value of 'k' and incrementing 'i'. More over second for loop iteration depends on value of 'k' which is decreasing through out the program hence we can see it narrows down in the output. This option is correct.

What is the output of C++ program given below?

#include<iostream> using namespace std; class Test { public: Test() { cout<<"constructor of Testn"; } ~Test() { cout<<"destructor of Testn"; } }; int main() { Test a; Test b; }

  1. constructor of Test constructor of Test destructor of Test destructor of Test

  2. constructor of Test destructor of Test constructor of Test destructor of Test

  3. constructor of Test constructor of Test

  4. constructor of Test destructor of Test

  5. None of the above


Correct Option: A
Explanation:

 Given class Test contains a default constructor & default destructor. We know constructor is called when we create instance for a class. Destructor is called when the instance variable doesnt point to any object or simply when it gets dereferenced. Test a;  //prints constructor of Test Test b;  //prints constructor of Test Now after end of the program, created instances are de-referenced. Hence prints destructor of Test (for instance 'a') prints destructor of Test (for instance 'b')

What is the output of C++ program given below?

#include<iostream> using namespace std; class A { public: A() { cout<<"Constructor of A"<<endl; } void Test(); }; void A::Test() { cout<<"This is Test"<<endl; } int main() { A a; a.Test();
}

  1. This is Test

  2. This is Test Constructor of A

  3. Constructor of A This is Test

  4. Error in the program

  5. None of the above


Correct Option: C
Explanation:

A a;    //creates object for class A, which calls default constructor Hence Constructor of A gets displayed first.a.Test();        //Now we call Test() using class object which prints message inside Test(). i.e This is Test As it matches with output given in this option , Hence true.

Which of the following statements is false regarding inline function in C++?

  1. All functions declared inside a class are inline functions by default.

  2. Inline functions can be declared outside a class by using inline keyword.

  3. Any modifications in an inline function needs recompilation of the program to use modified functionality.

  4. Declarartion of a function which is inside a class with inline keyword leads to compile time error.

  5. By declaring small functions (with less computation time) as inline function, performance of the program is increased.


Correct Option: D
Explanation:

This statement is false. We know if we declare a function inside a class, it is implicitly inline function. Eventhough we explicitly write inline keyword for a function inside a class, compiler does the same. It doesnt raise compile time error. Hence, this is a false statement & correct option to choose according to the question. 

What is the output of C++ program given below?

#include<iostream> using namespace std; class Parent1 { public: int a=10; }; class Parent2 { public: int a=5; }; class Child:public Parent1,public Parent2 { public: int func() { return a; } }; int main() { Child ch; cout<<ch.func(); }

  1. 10

  2. 5

  3. 10 5

  4. Compile time error

  5. None of the above


Correct Option: D
Explanation:

 When we compile the given program it generates compile time error.

Because integer variable 'a' is defined in both Parent classes. Child class inherits both Parent class & child class function func() accesses integer variable 'a' which leads to ambigious situation.

Error generated at linereturn a; Error message : reference to 'a' is ambigious This option is true.

What is the output of C++ program given below?

#include<iostream> using namespace std; class Parent { public: void func() { cout<<"function of Parent"<<endl; } }; class Child:public Parent { public: void func() { cout<<"function of Child"<<endl; } }; int main() { Child ch; ch.func(); }

  1. function of Parent

  2. function of Child

  3. function of Child function of Parent

  4. function of Child function of Parent

  5. Error in the program


Correct Option: B
Explanation:

Given program involves method overriding. Method Overriding: Using this feature, sub-class or child class can provide specific implementation of the method that is already provided by the parent class. In this program, func() is the method overriden by the child class as parent class already contains same method with same siganture fun().

Hence child class func() method overrides parent class func() method which prints message inside func() of child class only but not parent class. This option is true.

Choose the appropriate true statement among the given options with reference to C++ programming.

  1. Friend function of a class cannot access private members of the given class.

  2. It is not mandatory to have a pure virtual function(s) in an abstract class.

  3. We can create object for an abstract class.

  4. If a class inherits abstract class and doesn't implement pure virtual functions of base class, then derived class also becomes abstract class.

  5. None of the above


Correct Option: D
Explanation:

This option is true because the derived class doesn't implement all pure virtual functions of the abstract class(base class) it turns to abstract class itself. Again we have to inherit the derived class to another derived class where we must implement all the pure virtual functions. If we don't implement at present derived class it also turn out to abstract class. Finally an abstract class should be followed by a class where all implementations of pure virtual functions are done completely.

What is the output of C++ program given below?

#include<iostream> using namespace std; class Test { public: int a=9; void func() { cout<<a*a; }
}; class Hello:public Test { public: void func(int x) { cout<<a*x; } }; int main() { Hello obj; obj.func(5); }

  1. 81

  2. 45

  3. 25

  4. Error in the program

  5. None of the above


Correct Option: B
Explanation:

  Concept involved here in the program is function overloading. func is the signature common of the method which is present in both Test (parent class) & Hello (base class). But both vary in number of parameter which distinguishes them.

obj.func(5) which calls func(int x) hence output will be computed as follows: a*x=9*5=45 This option is true.

What is the output of C++ program given below?

#include<iostream> using namespace std; class Test { protected: int a=10,b=5; public: virtual int func()=0; }; class Imp:public Test { public: int func() { int x=++a*b++; return x;
} }; int main() { Imp i; cout<<i.func(); }

  1. 60

  2. 66

  3. 50

  4. 55

  5. Compile time error


Correct Option: D
Explanation:

 This program describes about abstract class. virtual int func()=0;     is a pure virtual function hence Test class becomes abstract class. Implementation of func() is present in Imp class where we are inheriting the abstract class Test into Imp class. int x=++a*b++; Initially a=10,b=5; ++a is pre-increment which makes value of 'a' as 11 & b++ is post-increment , until completion the statement execution 'b' value is 5. Only after completing the execution of the statement the value of 'b' is incremented. Hence 11*5=55: This option is correct.

What is the output of C++ program given below?

#include<iostream> using namespace std; class Test { public: mutable int a; /line1/ Test() /line2/ { a=10; /line3/ } void func() const /line4/ { a++; /line5/ cout<<a; } }; int main() { const Test obj; obj.func(); }

  1. Error at line5

  2. Error at line4

  3. Error at line3

  4. 11

  5. Error at line1


Correct Option: D
Explanation:

 This program works fine & there are no compile time errors. Mutable keyword is used to change the values of member variables eventhough the objects are declared as constant. In the program we declared constant object as follows: const Test obj; Now the obj is a constant object where its data members can't be changed. Only if data members are declared with mutable keyword are eligible for manipulation or changes. Hence value is incremented using a++ & output is displayed as 11.

Identify the type of inheritance used in the following C++ code snippet.

class A { }; class B:public A { }; class C:public A { }; class D:public A { };

  1. Multiple inheritance

  2. Single or simple inheritance

  3. Hierarchical inheritance

  4. Multilevel inheritance

  5. None of the above


Correct Option: C
Explanation:

Given code snippet contains single base class (A) nd three derived classes(B,C,D) from base class (A). It depicts hierarchical inheritance where a single base class is inherited by two or more derived classes. This option is true.

What is the output of C++ program given below?

#include<iostream> using namespace std; class Parent { public: int a=10,b=20;
}; class Middle:protected Parent { public: Middle() { a++; b--; }
}; class Child:private Middle { public: int hello() { return a*b; } }; int main() { Child ch; cout<<ch.hello();
}

  1. 200

  2. 209

  3. 189

  4. Compile time error

  5. None of the above


Correct Option: B
Explanation:

 class derived_class:access_specifier base_class We have to understand above syntax inorder to answer this question. when access_specifier is protected: all public & protected members of base_class becomes protected type for derived_class. class Middle:protected Parent  //int a,b are now protected Where Middle() constructor increments value of 'a' * decrements 'b' Hence a=11,b=19 when access_specifier is private: all public & protected of base_class becomes private members for derived_class. So, int a,b are now private for class Child Hence they are accessible to derived_class from the base_class. Which prints a*b=11*19=206

- Hide questions