Docsity

C++ Programming Concepts, Exams of Chemistry

Toggle sidebar
bg1
1 | P a g e
CPP Exam Questions and Answers 2024
Which one is not a coííect vaíiable
type in C++?
B.real
A.Float C.int D. double
Which opeíation is used as
Logical 'AND' C.Operator-&&A.Operator-& B.Operator-|| D. Operator +
A C++ code line ends
with ___
A.A Semicolon (;) B.A Fullstop(.) C.A Comma (,) D.A Slash (/)
Which class allows only one object to
be cíeated. C.SigletonA.Nuclear Family Class B.Abstruct Class Class D.Numero Uno Class
Reusability of code in C++ is achieved
thíough ____
A.Polymorphism B.Inheritance C.Encapsulation D.Both A and B
In CPP, membeís of a class aí e ______
by default.
B.PrivateA.Public C.Protected D.Static
In C++ Píogíam, inline fuctions aíe
expanded duíing ____
A.Run
Time
B.Compile Time C.Debug Time D.Coding Time
ľ o peífoí file input / output opeíation in C++, we must
include which headeí file ?
A.<fiostream> B.<ifstream> C.<ofstream> D.<fstream>
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download C++ Programming Concepts and more Exams Chemistry in PDF only on Docsity!

CPP Exam Questions and Answers 2024

Which one is not a coííect vaíiable

type in C++?

A.Float B.real C.int D. double

Which opeíation is used as

Logical 'AND'

A.Operator-& B.Operator-|| C.Operator-&& D. Operator +

A C++ code line ends

with ___

A.A Semicolon (;) B.A Fullstop(.) C.A Comma (,) D.A Slash (/)

Which class allows only one object to

be cíeated.

A.Nuclear Family Class B.Abstruct Class C.SigletonClass D.Numero Uno Class

Reusability of code in C++ is achieved

thíough ____

A.Polymorphism B.Inheritance C.Encapsulation D.Both A and B

In CPP, membeís of a class aíe ______

by default.

A.Public B.Private C.Protected D.Static

In C++ Píogíam, inline fuctions aíe

expanded duíing ____

A.Run

Time

B.Compile Time C.Debug Time D.Coding Time

ľ o peífoí file input / output opeíation in C++, we must

include which headeí file?

A. B. C. D.

1. Who invented C++?

a) Dennis Ritchie

b) Ken Thompson

c)Brian Kernighan

d) Bjarne Stroustrup

Answer: d

Explanation: Bjarne Stroustrup is the original creator of C++ in 1979 at

AT&T Bell Labs

2. What is C++?

C++ is an object oriented programming

language

a)

b) C++ is a procedural programming

language

c) C++ supports both procedural and object oriented programming

language d) C++ is a functional programming

language

Answer: c

Explanation: C++ supports both procedural(step by step instruction)

and object oriented programming (using the concept of classes and

objects).

3. Which of the following is used foí comments in C++?

a) /* comment */

b) // comment */

c)// comment

d) both // comment or /* comment */

Answer: d

Explanation: Both the ways are used for commenting in C++

programming. // is used for single line comments and /* … */ is used

for multiple line comments.

4. Which of the following useí-defined headeí file

extension used in c++?

a) hg

b) cpp

c)h

d) hf

Answer: c

Explanation: .h extensions are used for user defined header files. To

include a user defined header file one should use #include”name.h” i.e.

enclosed within double quotes.

  1. int *ptr = NULL;
  2. delete ptr;

#include

#include

using namespace std;

int main(int argc,

char const *argv[])

{

char s1[ 6 ] = "Hello";

char s2[ 6 ] =

"World";

char s3[ 12 ] = s1 + " " + s2;

cout<<s3;

return 0 ;

}

================code 1 =================

#include

using namespace std;

int main(int argc,

char const *argv[])

{

8. What happens if the following C++ statement is

compiled and executed?

The program is not semantically correct

b) The program is compiled and executed successfully

c)The program gives a compile-time error

d) The program compiled successfully but throws an error during run-

time

Answer: b

Explanation: The above statement is syntactically and semantically

correct as C++ allows the programmer to delete a NULL pointer,

therefore, the program is

compiled and executed successfully.

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

a) Hello

b) World

c)Error

d) Hello World

Answer: c

Explanation: There is no operation defined for the addition of character

array in C++ hence the compiler throws an error as it does not

understood what to do about this expression.

11. Which of the following C++ code will give eííoí on

compilation?

a)Code 1 only

b) Neither code 1 nor code 2

c) Both code 1 and code 2

d) Code 2 only

Answer: b

Explanation: Neither code 1 nor code 2 will give an error as both are

syntactically correct as in first code we have included namespace std and

in second one we have used scope resolution operator to resolve the

conflict.

12. By default, all the files in C++ aíe opened in _________

mode.

e) Binary

f) VTC

g)Text

h) ISCII

Answer: c

Explanation: By default, all the files in C++ are opened in text mode. They

read the file as normal text.

13. Which of the following coííectly declaíes an aííay in C+

i) array{10};

j) array array[10];

k)int array;

l) int array[10];

cout<<"Hello World";

return 0 ;

}

========================================

================code 2 =================

#include

int main(int argc, char const *argv[])

{

std::cout<<"Hello World";

return 0 ;

}

=========================

===============

#include

using namespace std;

int main()

{

c))

d) :

Answer: a

Explanation: ; semicolon is used to terminate a function declaration

statement in C++.

17. What is the coííect syntax of accessing a static

membeí of a class in C++?

Example class:

class A

{

public:

static int value;

}

e) A->value

f) A^value

g)A.value

h) A::value

Answer: d

Explanation: Scope resolution operator(::) is used to access a static

member of a class.

18. What is Inheíitance in C++?

i) Deriving new classes from existing classes

j) Overloading of classes

k)Classes with same names

l) Wrapping of data into a single class

Answer: a

Explanation: Inheritance is the concept of OOPs in which new classes are

derived from existing classes in order to reuse the properties of classes

defined earlier.

19. What will be the output of the

following C++ code?

$ g++ size3.cpp

$ a.out

4 5

#include

using namespace std;

void square (int *x, int *y)

{

x = (x) * --(*y);

}

int main ( )

{

int number = 30 ;

square(&number, &number);

cout << number;

return 0 ;

}

a) 2 5

b) 4 5

c) 4 6

d) 2 6

Answer: b

Explanation: The a as a integer will be converted to float while calculating

the size. The value of any variable doesn’t modify inside sizeof operator.

Hence value of variable a will remain 5.

Output:

20. What will be the output of the following

C++ code?

a) 30

b) Error

c)Segmentation

fault d) 870

Answer: d

Explanation: As we

are passing value

by reference

therefore the

change in the

value is

int a = 5 ;

float b;

cout << sizeof(++a + b);

cout << a;

return 0 ;

}

  1. #include
  2. using namespace std;

3.int operate (int a, int b) 4.

{

  1. return (a *

b); 6. }

  1. float operate (float a, float b)
    1. {
  2. return (a / b);
  3. }
  4. int main()
  5. {
  6. int x = 5 , y = 2 ;
  7. float n = 5.0, m =

2.0;

  1. cout << operate(x,

y) <<" \t ";

  1. cout << operate (n,

m);

  1. return 0 ;
  2. }

c)Assignment constructor

d) All of the mentioned

Answer: d

Explanation: If a programmer does not define the above constructors in a

class the C++ compiler by default provides these constructors to avoid error

on basic operations.

23. Which concept allows you to íeuse the wíitten code in C+

e) Inheritance

f) Polymorphism

g)Abstraction

h) Encapsulation

Answer: a

Explanation: Inheritance allows you to reuse your already written code by

inheriting the properties of written code into other parts of the code, hence

allowing you to reuse the already written code.

24. What will be the output of the following C++ code snippet?

a) 10.0 5

b) 10

2.5 c)

d) 5.0 2.

  1. $ g++ over3.cpp

20.$ a.out 21.

10 2.

  1. #include <stdio.h>
  2. #include
  3. using namespace std;
  4. int main ()
  5. {
  6. int array[] = { 0 , 2 , 4 , 6 , 7 , 5 , 3 };
  7. int n, result = 0 ;
  8. for (n = 0 ; n < 8 ; n++)
  9. {
  10. result += array[n];
  11. }
  12. cout << result;
  13. return 0 ;
  14. }

Answer: b

Explanation: In this program, we are divide and multiply the

values. Output:

25. How stíuctuíes and classes in C++ diffeí?

a) Structures by default hide every member whereas classes do not

b)In Structures, members are public by default whereas, in Classes, they are

private by default

c)Structures cannot have private members whereas classes can have

d)In Structures, members are private by default whereas, in Classes, they are

public by default

Answer: b

Explanation: Structure members are public by default whereas, class

members are private by default. Both of them can have private and

public members.

26. What will be the output of the following C++ code

snippet?

a)

b) 27

c) 26

d) 25

$ g++ op3.cpp

$ a.out

5 6

#include

using namespace std;

int main()

{

int i, j;

j = 10 ;

i = (j++,

j + 100 ,

999 + j);

cout << i;

return 0 ;

Answer: b

Explanation: Because the * operator is having highest precedence, So it is

executed first and then the + operator will be executed

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

  1. #include
  2. using namespace std;
  3. int main()
  4. {
  5. int a = 5 , b = 6 , c, d;
  6. c = a, b;
  7. d = (a, b);
  8. cout << c << ' ' << d;
  9. return 0 ;
  10. }

a) 5 6

b) 6 5

c) 6 7

d) 6 8

Answer: a

Explanation: It is a separator here. In C, the value a is stored in c and in d the

value b is stored in d because of the bracket.

Output:

31. What will be the output of the

following C++ code?

$ g++ op.cpp

$ a.out

749735

a)

b) 11

c)

d)

Answer

: c

Explan

ation: j

starts

with

the

value

  1. j is

then

increm

ented

to 11.

Next, j

is

added

to

  1. Finally, j (still containing 11) is added to 999 which yields the

result 1010. Output:

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

  1. #include
  2. using namespace std;
  3. int main ()
  4. {
  5. int x, y;
  6. x = 5 ;
  7. y = ++x * ++x;
  8. cout << x << y;
  9. x = 5 ;
  10. y = x++ * ++x;
  11. cout << x << y;
  12. return 0 ;
  13. }

a)

b)

c)

d)

Answer:

a

Explanation: Because of the precedence the pre-increment and post

increment operator, we got the output as 749736.

Output:

  1. }

$ g++ op2.cpp

$ a.out

1010

d) Polymorphism

Answer: c

Explanation: Inheritance allows you to reuse your already written code by

inheriting the properties of written code into other parts of the code, hence

allowing you to reuse the already written code.

37.How access specifieís in Class helps in Abstíaction?

a) They does not helps in any way

b) They allows us to show only required things to outer world

c)They help in keeping things together

d) Abstraction concept is not used in classes

Answer: b

Explanation: Abstraction is the concept of hiding things from the outer world

and showing only the required things to the world, which is where access

specifiers private, protected and public helps in keeping our knowledge

hidden from the world.

38. C++ is ______________

a) procedural programming language

b) object oriented programming language

c)functional programming language

d) both procedural and object oriented programming language

Answer: d

Explanation: C++ supports both procedural(step by step instruction)

and object oriented programming(using concept of classes and

objects).

39.What does modulaíity mean?

a) Hiding part of program

b) Subdividing program into small independent parts

c)Overriding parts of program

d) Wrapping things into single unit

Answer: b

Explanation: Modularity means dividing a program into independent sub

programs so that it can be invoked from other parts of the same program

or any other program.

class A

{

int i;

public:

void

print()

{cout<<

"hello"

<<i;}

}

class B:

public A

{

int j;

public:

void

assign(

int a)

{j =

a;}

}

40. Which of the following featuíe of OOPs is not

used in the following C++ code?

a) Abstraction

b) Encapsulation

c)Inheritance

d) Polymorphism

Answer: d

Explanation: As i and j members are private i.e. they are hidden from outer

world therefore we have used the concept of abstraction. Next data

members and there related functions are put together into single class

therefore encapsulation is used. Also as class B is derived from A therefore

Inheritance concept is used. But as no function is overloaded in any of the

classes therefore, the concept of polymorphism is missing here.

41.What is the otheí name used foí functions inside a class?

a) Member variables

b) Member functions

c)Class functions

d) Class variables

Answer:

b Explanation: Functions of a class are also known as member functions

of a class.

42.Why íefeíences aíe diffeíent fíom pointeís?

a) A reference cannot be made null

#include <iostream.h>

void func(void)

{

Cout<<("Hello");

}

void main()

{

func();

func( 2 );

}

Answer: a

Explanation: Boolean uses only 1 bit as it stores only truth values which can

be true(1) or false(0).

46.Which of the following is an exit-contíolled loop?

a) for

b) while

c)do-while

d) all of the mentioned

Answer: c

Explanation: do-while is called exit controlled loop because in do-while

termination condition is checked when we have executed the body of the

loop i.e. we are exiting the body and then checking the condition,

therefore, it is called exit controlled loop.

47.Which of the following is an entíy-contíolled loop?

a) for

b) while

c)do-while

d) both while and for

Answer: d

Explanation: Both while and for loops are called entry controlled loop

because in both of them the termination condition is checked before we

enter the body of the loop hence they are called entry controlled loop.

48. What happens if the following píogíam is executed in C

and C++?

#include

using namespace std;

int x[ 100 ];

int main()

{

cout << x[ 99 ] <<

endl;

}

a) Error in both C and C++

b) Outputs Hello twice in both C and C++

c)Error in C and successful execution in C++

d) Error in C++ and successful execution in C

Answer: a

Explanation: As the func(void) needs no argument during its call, hence

when we are calling func(2) with 2 as passed as a parameter then this

statement gives the error in both C++ and C compiler

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

a) Garbage value

b) 0

c) 99

d) Error

Answer: b

Explanation: In C++ all the uninitialized variables are set to 0 therefore the

value of all elements of the array is set to 0.

50.Which of the following is accessed by a membeí 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.

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