Play With C

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 71

1. Are logical operator sequence points?

a) True
b) False
c) Depends on the compiler
d) Depends on the standard

Answer: a

2. Do logical operators in the C language are evaluated with the short circuit?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard

Answer: a

3. Result of a logical or relational expression in C is _________


a) True or False
b) 0 or 1
c) 0 if an expression is false and any positive number if an expression is true
d) None of the mentioned

Answer: b

4. What will be the value of d in the following program?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 5, c = 5;
5. int d;
6. d = b + c == a;
7. printf("%d", d);
8. }

a) Syntax error
b) 1
c) 5
d) 10
View Answer
Answer: b

5. What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 10, b = 5, c = 3;
5. b != !a;
6. c = !!a;
7. printf("%d\t%d", b, c);
8. }

a) 5 1
b) 0 3
c) 5 3
d) 1 1
View Answer
Answer: a

6. Which among the following is NOT a logical or relational operator?


a) !=
b) ==
c) ||
d) =
View Answer
Answer: d

7. What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 10;
5. if (a == a--)
6. printf("TRUE 1\t");
7. a = 10;
8. if (a == --a)
9. printf("TRUE 2\t");
10. }

a) TRUE 1
b) TRUE 2
c) TRUE 1 TRUE 2
d) Compiler Dependent

Answer: d

8. Relational operators cannot be used on ____________


a) structure
b) long
c) strings
d) float
Answer: a

9. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int a = 5 * 3 + 2 - 4;
5. printf("%d", a);
6. }

a) 13
b) 14
c) 12
d) 1 6
View Answer
Answer: a

10. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int a = 2 + 4 + 3 * 5 / 3 - 5;
5. printf("%d", a);
6. }

a) 7
b) 6
c) 10
d) 9
View Answer
Answer: b

11. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int a = 5 * 3 % 6 - 8 + 3;
5. printf("%d", a);
6. }

a) 10
b) 2
c) -2
d) -3
View Answer
Answer: c

12. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int b = 6;
5. int c = 7;
6. int a = ++b + c--;
7. printf("%d", a);
8. }

a) Run time error


b) 15
c) 13
d) 14
Answer: d

13. What is the output of this C code?


1. #include <stdio.h>
2. void main(
3. {
4. double b = 8;
5. b++;
6. printf("%lf", b);
7. }

a) 9.000000
b) 9
c) 9.0
d) Run time error

Answer: a

14. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. double b = 3 % 0 * 1 - 4 / 2;
5. printf("%lf", b);
6. }

a) -2
b) Floating point Exception
c) 1
d) None of the mentioned

Answer: b

15. What is the type of the below assignment expression if x is of type float, y is of type int?
y = x + y;

a) int
b) float
c) there is no type for an assignment expression
d) double

Answer: a

16. What is the value of the below assignment expression?


(x = foo())!= 1 considering foo() returns 2

a) 2
b) True
c) 1
d) 0

Answer: a

17. Operation “a = a * b + a” can also be written as ________


a) a *= b + 1;
b) (c = a * b)!=(a = c + a);
c) a = (b + 1)* a;
d) All of the mentioned

Answer: d

18. for c = 2, value of c after c <<= 1;


a) c = 1;
b) c = 2;
c) c = 3;
d) c = 4;
View Answer
Answer: d

19. What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 1, b = 2;
5. a += b = a;
6. printf("%d %d", a, b);
7. }

a) 1 1
b) 1 2
c) 2 1
d) 2 2

Answer: c

20. What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 4, n, i, result = 0;
5. scanf("%d", n);
6. for (i = 0;i < n; i++)
7. result += a;
8. }

a) Addition of a and n
b) Subtraction of a and n
c) Multiplication of a and n
d) Division of a and n

Answer: c

21. Which of the following is an invalid assignment operator?


a) a %= 10;
b) a /= 10;
c) a |= 10;
d) None of the mentioned

Answer: d

22. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int a = 5 * 3 + 2 - 4;
5. printf("%d", a);
6. }

a) 13
b) 14
c) 12
d) 1 6

Answer: a

23. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int a = 2 + 4 + 3 * 5 / 3 - 5;
5. printf("%d", a);
6. }

a) 7
b) 6
c) 10
d) 9

Answer: b

24. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int a = 5 * 3 % 6 - 8 + 3;
5. printf("%d", a);
6. }

a) 10
b) 2
c) -2
d) -3

Answer: c

25. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. int b = 6;
5. int c = 7;
6. int a = ++b + c--;
7. printf("%d", a);
8. }

a) Run time error


b) 15
c) 13
d) 14

Answer: d

26. What is the output of this C code?


1. #include <stdio.h>
2. void main(
3. {
4. double b = 8;
5. b++;
6. printf("%lf", b);
7. }

a) 9.000000
b) 9
c) 9.0
d) Run time error

Answer: a

27. What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. double b = 3 % 0 * 1 - 4 / 2;
5. printf("%lf", b);
6. }

a) -2
b) Floating point Exception
c) 1
d) None of the mentioned

Answer: b

28. The function srand(unsigned)


a) Sets the seed for rand
b) Doesn’t exist
c) Is an error
d) None of the mentioned

Answer: a

29. Which is the best way to generate numbers between 0 to 99?


a) rand()-100
b) rand()%100
c) rand(100)
d) srand(100)

Answer: b

30. The correct way to generate numbers between minimum and maximum(inclusive) is
_________________
a) minimum + (rand() % (maximum – minimum));
b) minimum + (rand() % (maximum – minimum + 1));
c) minimum * (rand() % (maximum – minimum))
d) minimum – (rand() % (maximum+minimum));
Answer: b

31. rand() and srand() functions are used


a) To find sqrt
b) For and operations
c) For or operations
d) To generate random numbers

Answer: d

32. What is the return type of rand() function?


a) short
b) int
c) long
d) double

Answer: b

33. Which of the following snippet will effectively generate random numbers?
a) rand();
b) rand(10);
c) rand(time(NULL));
d) all of the mentioned

Answer: a

34. Which among the following is correct function call for rand() and random()?
a) rand() and random();
b) rand() and random(1);
c) rand(1) and random(1);
d) rand(1) and random();

Answer: a

35. For the function call time(), what type of parameter is accepted?
a) int
b) int *
c) time_t
d) time_t *

Answer: d

36. What is the output of this C code?


1. #include <stdio.h>
2. void foo(const int *);
3. int main()
4. {
5. const int i = 10;
6. printf("%d ", i);
7. foo(&i);
8. printf("%d", i);
9.
10. }
11. void foo(const int *i)
12. {
13. *i = 20;
14. }

a) Compile time error


b) 10 20
c) Undefined value
d) 10

Answer: a

37. Comment on the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. const int i = 10;
5. int *ptr = &i;
6. *ptr = 20;
7. printf("%d\n", i);
8. return 0;
9. }
a) Compile time error
b) Compile time warning and printf displays 20
c) Undefined behaviour
d) 10

Answer: b

38. What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. j = 10;
5. printf("%d\n", j++);
6. return 0;
7. }

a) 10
b) 11
c) Compile time error
d) 0

Answer: c

39. Does this compile without error?


1. #include <stdio.h>
2. int main()
3. {
4. for (int k = 0; k < 10; k++);
5. return 0;
6. }

a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) None of the mentioned

Answer: c
39)/*Program Snippet 1 with for loop*/

for (i = 0; i < 10; i++)


{
/*statement1*/
continue;
/*statement2*/
}

/*Program Snippet 2 with while loop*/


i = 0;
while (i < 10)
{
/*statement1*/
continue;
/*statement2*/
i++;
}

/*Program Snippet 3 with do-while loop*/


i = 0;
do
{
/*statement1*/
continue;
/*statement2*/
i++;
}while (i < 10);

(A) All the loops are equivalent i.e. any of the three can be chosen and they all will perform
exactly same.
(B) continue can’t be used with all the three loops in C.
(C) After hitting the continue; statement in all the loops, the next expression to be executed
would be controlling expression (i.e. i < 10) in all the 3 loops.
(D) None of the above is correct.

Answer: (D)

40) In the context of “break” and “continue” statements in C, pick the best statement.
(A) “break” can be used in “for”, “while” and “do-while” loop body.
(B) “continue” can be used in “for”, “while” and “do-while” loop body.
(C) “break” and “continue” can be used in “for”, “while”, “do-while” loop body and
“switch” body.
(D) “break” and “continue” can be used in “for”, “while” and “do-while” loop body. But
only “break” can be used in “switch” body.
(E) “break” and “continue” can be used in “for”, “while” and “do-while” loop body. Besides,
“continue” and “break” can be used in “switch” and “if-else” body.

Answer: (D)

40)What would happen when we compile and run this program?

#include "stdio.h"
int main()
{
int i;
goto LOOP;
for (i = 0 ; i < 10 ; i++)
{
printf("GeeksQuiz\n");
LOOP:
break;
}
return 0;
}
(A) No compile error and it will print GeeksQuiz 10 times because goto label LOOP wouldn’t
come in effect.
(B) No compile error and it’ll print GeeksQuiz only once because goto label LOOP would come
in picture only after entering for loop.
(C) Compile Error because any goto label isn’t allowed in for loop in C.
(D) No compile error but behaviour of the program would depend on C compiler due to
nondeterministic behaviour of goto statement.
(E) No compile error and it will not print anything.

Answer: (E)

41)A typical “switch” body looks as follows:

switch (controlling_expression)
{
case label1:
/*label1 statements*/
break;
case label2:
/*label1 statements*/
break;
default:
/*Default statements*/
}

Which of the following statement is not correct statement?


(A) “switch” body may not have any “case” label at all and it would still compile.
(B) “switch” body may not have the “default” label and it would still compile.
(C) “switch” body may contain more than one “case” labels where the label value of these “case”
is same and it would still compile. If “switch” controlling expression results in this “case” label
value, the “case” which is placed first would be executed.
(D) “switch” body may not have any “break” statement and it would still compile.
(E) “switch” body can have the “default” label at first i.e. before all the other “case” labels. It
would still compile.

Answer: (C)
42)Which of the following is correct with respect to “Jump Statements” in C?
(A) goto
(B) continue
(C) break
(D) return
(E) All of the above.

Answer: (E)

43)Assuming int size is 4 bytes, what is going to happen when we compile and run the
following program?

#include “stdio.h”
int main()
{
printf(“GeeksQuiz\n”);
main();
return 0;
}

(A) We can’t use main() inside main() and compiler will catch it by showing compiler error.
(B) GeeksQuiz would be printed in 2147483647 times i.e. (2 to the power 31) – 1.
(C) It’ll print GeeksQuiz infinite times i.e. the program will continue to run forever until it’s
terminated by other means such as CTRL+C or CTRL+Z etc.
(D) GeeksQuiz would be printed only once. Because when main() is used inside main(), it’s
ignored by compiler at run time. This is to make sure that main() is called only once.
(E) GeeksQuiz would be printed until stack overflow happens for this program.

Answer: (E)

44)What’s the meaning of following declaration in C language?

int (*p)[5];

(A) It will result in compile error because there shouldn’t be any parenthesis i.e. “int *p[5]” is
valid.
(B) p is a pointer to 5 integers.
(C) p is a pointer to integer array.
(D) p is an array of 5 pointers to integers.
(E) p is a pointer to an array of 5 integers

Answer: (E)

45)In a C program, following variables are defined:

float x = 2.17;
double y = 2.17;
long double z = 2.17;

Which of the following is correct way for printing these variables via printf.
(A) printf(“%f %lf %Lf”,x,y,z);
(B) printf(“%f %f %f”,x,y,z);
(C) printf(“%f %ff %fff”,x,y,z);
(D) printf(“%f %lf %llf”,x,y,z);

Answer: (A)

46) - One of the following is true for an inline function.

A - It executes faster as it is treated as a macro internally

B - It executes faster because it priority is more than normal function

C - It doesn’t executes faster compared to a normal function

D - None of the above holds true for an inline function

47) - Which operator is required to be overloaded as member function only?

A - _

B - _ _

C - ++ (postfix version)

D - =

48) - Which is the storage specifier used to modify the member variable even though the class
object is a constant object?
A - auto

B - register

C - static

D – mutable

49) - What is the output of the following program?

#include<iostream>

using namespace std;

class Base {

public:

virtual void f() {

cout<<"Base\n";

};

class Derived:public Base {

public:

void f() {

cout<<"Derived\n";

};

main() {

Base *p = new Derived();


p->f();

A - Base

B - Derived

C - Compile error

D - None of the above.

50)- Which operator is used to resolve the scope of the global variable?

A - −>

B - .

C - *

D - ::

51) - Choose the option not applicable for the constructor.

A - Cannot be called explicitly.

B - Cannot be overloaded.

C - Cannot be overridden.

D - None of the above.

52) - An exception is __

A - Runtime error

B - Compile time error

C - Logical error

D - None of the above


53) - What is the size of ‘int’?

A - 2

B - 4

C - 8

D - Compiler dependent

54)- Special symbol permitted with in the identifier name.

A - $

B - @

C - _

D - .

55) - What is the output of the following program?

#include<iostream>

using namespace std;

void main() {

char *s = "C++";

cout<<s<<" ";

s++;

cout<<s<<" ";

A - C++ C++


B - C++ ++

C - ++ ++

D - Compile error

56) constructor that accepts __________ parameters is called the default constructor.
A
one
.

B. two

C
no
.

D
three
.
Answer: Option C

57)What happens when a class with parameterized constructors and having no default
constructor is used in a program and we create an object that needs a zero-argument
constructor?
A
Compile-time error.
.

B. Preprocessing error.

C
Runtime error.
.

D
Runtime exception.
.
Answer: Option A

58) Can a class have virtual destructor?


A
Yes
.

B. No
Answer: Option A

59) Destructor has the same name as the constructor and it is preceded by ______ .
A !
.

B. ?

C
~
.

D
$
.
Answer: Option C

60) For automatic objects, constructors and destructors are called each time the objects
A
enter and leave scope
.

B. inherit parent class

C
are constructed
.

D
are destroyed
.
Answer: Option A

61) Which constructor function is designed to copy objects of the same class type?
A
Create constructor
.

B. Object constructor

C
Dynamic constructor
.

D
Copy constructor
.
Answer: Option D

62) Which of the following statement is correct?


A
Constructor has the same name as that of the class.
.

Destructor has the same name as that of the class with a tilde symbol at the
B.
beginning.

C
Both A and B.
.
D
Destructor has the same name as the first member function of the class.
.
Answer: Option C

63) Which of the following statement is incorrect?


A
Constructor is a member function of the class.
.

B. The compiler always provides a zero argument constructor.

C
It is necessary that a constructor in a class should always be public.
.

D
Both B and C.
.
Answer: Option D

64) When are the Global objects destroyed?


A
When the control comes out of the block in which they are being used.
.

B. When the program terminates.

C
When the control comes out of the function in which they are being used.
.

D
As soon as local objects die.
.
Answer: Option B

65) Copy constructor must receive its arguments by __________ .


A
either pass-by-value or pass-by-reference
.

B. only pass-by-value

C
only pass-by-reference
.

D
only pass by address
.
Answer: Option C

66) Which of the following function prototype is perfectly acceptable?


. A
int Function(int Tmp = Show());
.

B. float Function(int Tmp = Show(int, float));

C
Both A and B.
.

D
float = Show(int, float) Function(Tmp);
.
Answer: Option A

67) Which of the following statement is correct?


A
C++ enables to define functions that take constants as an argument.
.

B. We cannot change the argument of the function that that are declared as constant.

C
Both A and B.
.

D
We cannot use the constant while defining the function.
.
Answer: Option C

68) Which of the following statement is correct?


A
Overloaded functions can have at most one default argument.
.

B. An overloaded function cannot have default argument.

C
All arguments of an overloaded function can be default.
.

D
A function if overloaded more than once cannot have default argument.
.
Answer: Option C

69)Which of the following statement is correct?


A Two functions having same number of argument, order and type of argument can
. be overloaded if both functions do not have any default argument.

B. Overloaded function must have default arguments.

C Overloaded function must have default arguments starting from the left of
. argument list.

D
A function can be overloaded more than once.
.
Answer: Option D

70) Which of the following statement will be correct if the function has three arguments
passed to it?
A
The trailing argument will be the default argument.
.

B. The first argument will be the default argument.

C
The middle argument will be the default argument.
.

D
All the argument will be the default argument.
.
Answer: Option A
71) Which of the following statement is incorrect?
A
Default arguments can be provided for pointers to functions.
.

B. A function can have all its arguments as default.

C
Default argument cannot be provided for pointers to functions.
.

D
A default argument cannot be redefined in later declaration.
.
Answer: Option C

72) Which of the following statement is correct?


A
Constructors can have default parameters.
.
B. Constructors cannot have default parameters.

C
Constructors cannot have more than one default parameter.
.

D
Constructors can have at most five default parameters.
.
Answer: Option A

73) Which of the following function / type of function cannot be overloaded?


. A
Member function
.

B. Static function

C
Virtual function
.

D
Both B and C
.
Answer: Option C

74) Which of the following function declaration is/are incorrect?


A
int Sum(int a, int b = 2, int c = 3);
.

B. int Sum(int a = 5, int b);

C
int Sum(int a = 0, int b, int c = 3);
.

D
Both B and C are incorrect.
.

E. All are correct.


Answer: Option D

75) Which of the following statement is incorrect?


. A The default value for an argument can be a global constant.
.

B. The default arguments are given in the function prototype.

C
Compiler uses the prototype information to build a call, not the function definition.
.

D The default arguments are given in the function prototype and should be repeated in
. the function definition.
Answer: Option D

76) What happens when we try to compile the class definition in following code snippet?
class Birds {};
class Peacock : protected Birds {};

A
It will not compile because class body of Birds is not defined.
.

B. It will not compile because class body of Peacock is not defined.

C
It will not compile because a class cannot be protectedly inherited from other class.
.

D
It will compile succesfully.
.
Answer: Option D

77) Which of the following statements is incorrect?


A
Friend keyword can be used in the class to allow access to another class.
.

B. Friend keyword can be used for a function in the public section of a class.

C
Friend keyword can be used for a function in the private section of a class.
.

D
Friend keyword can be used on main().
.
Answer: Option D
78) Which of the following statement is correct regarding destructor of base class?
A
Destructor of base class should always be static.
.

B. Destructor of base class should always be virtual.

C
Destructor of base class should not be virtual.
.

D
Destructor of base class should always be private.
.
Answer: Option B

79) Which of the following two entities (reading from Left to Right) can be connected by the dot
operator?
A
A class member and a class object.
.

B. A class object and a class.

C
A class and a member of that class.
.

D
A class object and a member of that class.
.
Answer: Option D

80) How can we make a class abstract?


A
By making all member functions constant.
.

B. By making at least one member function as pure virtual function.

C
By declaring it abstract using the static keyword.
.

D
By declaring it abstract using the virtual keyword.
.
Answer: Option B

81) Which of the following statements is correct when a class is inherited publicly?
A
Public members of the base class become protected members of derived class.
.

B. Public members of the base class become private members of derived class.

C
Private members of the base class become protected members of derived class.
.

D
Public members of the base class become public members of derived class.
.
Answer: Option D

82) Which of the following statements is correct about the constructors and destructors?
A
Destructors can take arguments but constructors cannot.
.

B. Constructors can take arguments but destructors cannot.

C
Destructors can be overloaded but constructors cannot be overloaded.
.

D
Constructors and destructors can both return a value.
.
Answer: Option B

83) Which of the following access specifies is used in a class definition by default?
A
Protected
.

B. Public

C
Private
.

D
Friend
.
Answer: Option C

84) Which of the following statement is correct with respect to the use of friend keyword
inside a class?
A
A private data member can be declared as a friend.
.

B. A class may be declared as a friend.

C
An object may be declared as a friend.
.

D
We can use friend keyword as a class name.
.
Answer: Option B

85)Which of the following keywords is used to control access to a class member?


A
Default
.

B. Break

C
Protected
.

D
Asm
.
Answer: Option C

86)Which of the following statements is correct when a class is inherited publicly?


A
Public members of the base class become protected members of derived class.
.

B. Public members of the base class become private members of derived class.

C
Private members of the base class become protected members of derived class.
.

D
Public members of the base class become public members of derived class.
.
Answer: Option D

87) Which of the following statements is correct about the constructors and destructors?
A
Destructors can take arguments but constructors cannot.
.

B. Constructors can take arguments but destructors cannot.

C
Destructors can be overloaded but constructors cannot be overloaded.
.

D
Constructors and destructors can both return a value.
.
Answer: Option B

88) Which of the following access specifies is used in a class definition by default?
A
Protected
.

B. Public

C
Private
.

D
Friend
.
Answer: Option C

89) Which of the following statement is correct with respect to the use of friend keyword
. inside a class?
A
A private data member can be declared as a friend.
.

B. A class may be declared as a friend.

C
An object may be declared as a friend.
.

D
We can use friend keyword as a class name.
.
Answer: Option B

90) Which of the following keywords is used to control access to a class member?
A
Default
.

B. Break

C
Protected
.

D
Asm
.
Answer: Option C

91) . A default catch block catches

A
all thrown objects
.

B.no thrown objects

C
any thrown object that has not been caught by an earlier catch block
.

D
all thrown objects that have been caught by an earlier catch block
.

Answer & Explanation


Answer: C

92) Format flags may be combined using

A
the bitwise OR operator (|)
.

B.the logical OR operator (||)


C
the bitwise AND operator (&)
.

D
the logical AND operator (&&)
.

Answer & Explanation

Answer: A

93). The use of the break statement in a switch statement is

A
optional
.

B.compulsory

C
not allowed. It gives an error message
.

D
to check an error
.

Answer: A

94) To expose a data member to the program, you must declare the data member in the _____
section of the class

A. common B. exposed

C. public D. unrestricted

Answer: C
95). Which of the following are valid characters for a numeric literal constant?

A
a comma
.

B.a dollar sign ($)

C
a percent sign (%)
.

D
None of the above
.

Answer: D

96). A function that changes the state of the cout object is called a(n) _____

A. member B. adjuster

C. manipulator D. operator

Answer: C

97) A C++ program contains a function with the header int function(double d, char c). Which of
the following function headers could be used within the same program?

A
char function(double d, char c)
.

B.int function(int d, char c)


C
both (a) and (b)
.

D
neither (a) nor (b)
.

Answer: B

98) When the compiler cannot differentiate between two overloaded constructors, they are called

A. overloaded B. destructed

C. ambiguous D. dubious

Answer: C

99) If you design a class that needs special initialization tasks, you will want to design a(n)
_____

A
housekeeping routine
.

B.initializer

C
constructor
.

D
compiler
.

Answer: C
100) Which type of statement does not occur in computer programs?

A. sequence B. loop

C. denial D. selection

Answer: C

101)#include<iostream>
using namespace std;

class A
{
// data members of A
public:
A ()           { cout << "\n A's constructor"; /* Initialize data members */ }
A (const A &a) { cout << "\n A's Copy constructor";  /* copy data members */}
A& operator= (const A &a) // Assignemt Operator
{
// Handle self-assignment:
if(this == &a) return *this;

// Copy data members


cout << "\n A's Assignment Operator";  return *this;
}
};

class B
{
A a;
// Other members of B
public:
B(A &a) { this->a = a; cout << "\n B's constructor"; }
};

int main()
{
A a1;
B b(a1);
return 0;
}
Answer:

a). A's constructor


A's constructor
A's Assignment Operator
B's constructor

b) compile time error


c) runtime error

d) A's constructor
A's constructor
B's constructor
A's Assignment Operator

102)Which one of the following is an application of Stack Data Structure?


(A) Managing function calls
(B) The stock span problem
(C) Arithmetic expression evaluation
(D) All of the above

Answer: (D)

103) Which of the following is true about linked list implementation of stack?
(A) In push operation, if new nodes are inserted at the beginning of linked list, then in pop
operation, nodes must be removed from end.
(B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be
removed from the beginning.
(C) Both of the above
(D) None of the above
Answer: (D) 

104)declare a stack of characters


while ( there are more characters in the word to read )
{
read a character
push the character on the stack
}
while ( the stack is not empty )
{
pop a character off the stack
write the character to the screen
}

What is output for input “geeksquiz”?


(A) geeksquizgeeksquiz
(B) ziuqskeeg
(C) geeksquiz
(D) ziuqskeegziuqskeeg

Answer: (B)

104) The following postfix expression with single digit operands is evaluated using a stack:

823^/23*+51*-

Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is
evaluated are:
(A) 6, 1
(B) 5, 7
(C) 3, 2
(D) 1, 5

Answer: (A)
105) Assume that the operators +, -, × are left associative and ^ is right associative. The order of
precedence (from highest to lowest) is ^, x , +, -. The postfix expression corresponding to the
infix expression a + b × c – d ^ e ^ f is
(A) abc × + def ^ ^ –
(B) abc × + de ^ f ^ –
(C) ab + c × d – e ^ f ^
(D) – + a × bc ^ ^ def

Answer: (A) 

106)- What is the output of the following program?

#include<stdio.h>

main()

register int x = 5;

int *p;

p=&x;

x++;

printf("%d",*p);

A - Compile error

B - 5

C - 6

D - Garbage value


107) - What is the size of ‘int’?

A - 2

B - 4

C - 8

D - Compiler dependent

108) - What is the output of the following program?

#include<stdio.h>

main()

int i = 1;

Charminar:

printf("%d ",i++);

if(i==3) break;

if(i<=5) goto Charminar;

A - 1 2

B - 1 2 3

C - 1 2 4 5

D - Compile error

109) - Does both the loops in the following programs prints the correct string length?

#include<stdio.h>
main()

int i;

char s[] = "hello";

for(i=0; s[i]; ++i);

printf("%d ", i);

i=0;

while(s[i++]);

printf("%d ", i);

A - Yes, both the loops prints the correct length

B - Only for loop prints the correct length

C - Only while loop prints the correct length

D - Compile error in the program.

110) - What is the output of the following statement?

#include<stdio.h>

main()

printf("%d", !0<2);

}
A - 0

B - 1

C - False

D - True

111) - What is (void*)0?

A - Symbolize the NULL pointer

B - Symbolize the void pointer

C - Symbolize both, NULL & void pointer

D - Many display error

112)- Which header file supports the functions - malloc() and calloc()?

A - stdlib.h

B - memory.h

C - math.h

D - stdio.h

113) - Choose the correct statement that is a combination of these two statements,

Statement 1: char *p;

Statement 2: p = (char*) malloc(100);

A - char p = *malloc(100);

B - char *p = (char*)malloc(100);

C - char *p = (char) malloc(100);

D - None of the above


114) - What will be the output of the following program?

#include<stdio.h>

int main()

const int i = 0;

printf("%d\n", i++);

return 0;

A - 100

B - Infinity

C - 0

D - Return error

115) - What will be the output of the given below code?

#include<stdio.h>

int main()

const int *ptr = &i;

char str[] = "Welcome";

s = str;
while(*s)

printf("%c", *s++);

return 0;

A - Welcome

B - 0

C - Wel

D - Come

116) - What is the output of the following program?

#include<stdio.h>

main()

register int x = 5;

int *p;

p=&x;

x++;

printf("%d",*p);

A - Compile error

B - 5

C - 6

D - Garbage value


117) - What is the size of ‘int’?

A - 2

B - 4

C - 8

D - Compiler dependent

118) - What is the output of the following program?

#include<stdio.h>

main()

int i = 1;

Charminar:

printf("%d ",i++);

if(i==3) break;

if(i<=5) goto Charminar;

A - 1 2

B - 1 2 3

C - 1 2 4 5

D - Compile error

119) - Does both the loops in the following programs prints the correct string length?
#include<stdio.h>

main()

int i;

char s[] = "hello";

for(i=0; s[i]; ++i);

printf("%d ", i);

i=0;

while(s[i++]);

printf("%d ", i);

A - Yes, both the loops prints the correct length

B - Only for loop prints the correct length

C - Only while loop prints the correct length

D - Compile error in the program.

120) - What is the output of the following statement?

#include<stdio.h>

main()

printf("%d", !0<2);
}

A - 0

B - 1

C - False

D - True

121) - What is (void*)0?

A - Symbolize the NULL pointer

B - Symbolize the void pointer

C - Symbolize both, NULL & void pointer

D - Many display error

122) - What will be the output of the following program?

#include<stdio.h>

int main()

const int i = 0;

printf("%d\n", i++);

return 0;

A - 100

B - Infinity

C - 0
123) - Return error
1. Which of the following correctly declares an array?
a) int array[10];
b) int array;
c) array{10};
d) array array[10];

124) What will be the output of this program?


1. #include <stdio.h>
2. using namespace std;
3. int array1[] = {1200, 200, 2300, 1230, 1543};
4. int array2[] = {12, 14, 16, 18, 20};
5. int temp, result = 0;
6. int main()
7. {
8. for (temp = 0; temp < 5; temp++)
9. {
10. result += array1[temp];
11. }
12. for (temp = 0; temp < 4; temp++)
13. {
14. result += array2[temp];
15. }
16. cout << result;
17. return 0;
18. }
a) 6553
b) 6533
c) 6522
d) 12200
6533

125) What will be the output of the this program?


1. #include <stdio.h>
2. using namespace std;
3. int main ()
4. {
5. int array[] = {0, 2, 4, 6, 7, 5, 3};
6. int n, result = 0;
7. for (n = 0; n < 8; n++) {
8. result += array[n];
9. }
10. cout << result;
11. return 0;
12. }
a) 25
b) 26
c) 27
d) None of the mentioned

126)What is the output of this program?


1. #include <stdio.h>
2. using namespace std;
3. int main()
4. {
5. int a = 5, b = 10, c = 15;
6. int arr[3] = {&a, &b, &c};
7. cout << *arr[*arr[1] - 8];
8. return 0;
9. }
a)15
b)18
c)garbagevalue
d) compile time error

127). There are how many types of linkage there in c++?


a) 1
b) 2
c) 3
d) 4

128). To use internal linkage we have to use which keyword?


a) static
b) extern
c) static or extern
d) none of the mentioned
129) What will be the output of these two programs?
1.
1. #ifndef Exercise_H
2. #define Exercise_H
3. int num = 842;
4. #endif

2.
1. #include <iostream>
2. #include "exe.h"
3. using namespace std;
4. int main(int argc, char * argv[] )
5. {
6. cout << number++;
7. return 0;
8. }

a)842
b)843
c)compiletimeerror
d) none of the mentioned

130). What is the default type of linkage that is available for identifiers?
a)internal
b)external
c)nolinkage
d) none of the mentioned

131) Which of the following performs deletion of the last element in the list? Given below is the
Node class.
class Node
{
protected Node next;
protected Object ele;
Node(Object e,Node n)
{
ele = e;
next = n;
}
public void setNext(Node n)
{
next = n;
}
public void setEle(Object e)
{
ele = e;
}
public Node getNext()
{
return next;
}
public Object getEle()
{
return ele;
}
}
class SLL
{
Node head;
int size;
SLL()
{
size = 0;
}
}

a)
public Node removeLast()
{
if(size == 0)
return null;
Node cur;
Node temp;
cur = head;
while(cur.getNext() != null)
{
temp = cur;
cur = cur.getNext();
}
temp.setNext(null);
size--;
return cur;
}

b)
public void removeLast()
{
if(size == 0)
return null;
Node cur;
Node temp;
cur = head;
while(cur != null)
{
temp = cur;
cur = cur.getNext();
}
temp.setNext(null);
return cur;
}

c)
public void removeLast()
{
if(size == 0)
return null;
Node cur;
Node temp;
cur = head;
while(cur != null)
{
cur = cur.getNext();
temp = cur;
}
temp.setNext(null);
return cur;
}

d)
public void removeLast()
{
if(size == 0)
return null;
Node cur;
Node temp;
cur = head;
while(cur.getNext() != null)
{
cur = cur.getNext();
temp = cur;
}
temp.setNext(null);
return cur;
}

 
132). What is the functionality of the following code?
public void function(Node node)
{
if(size == 0)
head = node;
else
{
Node temp,cur;
for(cur = head; (temp = cur.getNext())!=null; cur = temp);
cur.setNext(node);
}
size++;
}

a) Inserting a node at the beginning of the list


b) Deleting a node at the beginning of the list
c) Inserting a node at the end of the list
d) Deleting a node at the end of the list

133) What is the space complexity for deleting a linked list?


a)O(1)
b)O(n)
c)Either O(1) or O(n)
d) O(logn)

134) How would you delete a node in the singly linked list? The position to be deleted is given.
a)
public void delete(int pos)
{
if(pos < 0)
pos = 0;
if(pos > size)
pos = size;
if( size == 0)
return;
if(pos == 0)
head = head.getNext();
else
{
Node temp = head;
for(int i=1; i<pos; i++)
{
temp = temp.getNext();
}
temp.setNext(temp.getNext().getNext());
}
size--;
}

b)
public void delete(int pos)
{
if(pos < 0)
pos = 0;
if(pos > size)
pos = size;
if( size == 0)
return;
if(pos == 0)
head = head.getNext();
else
{
Node temp = head;
for(int i=1; i<pos; i++)
{
temp = temp.getNext();
}
temp.setNext(temp.getNext());
}
size--;
}

c)
public void delete(int pos)
{
if(pos < 0)
pos = 0;
if(pos > size)
pos = size;
if( size == 0)
return;
if(pos == 0)
head = head.getNext();
else
{
Node temp = head;
for(int i=1; i<pos; i++)
{
temp = temp.getNext().getNext();
}
temp.setNext(temp.getNext().getNext());
}
size--;
}

d)
public void delete(int pos)
{
if(pos < 0)
pos = 0;
if(pos > size)
pos = size;
if( size == 0)
return;
if(pos == 0)
head = head.getNext();
else
{
Node temp = head;
for(int i=0; i<pos; i++)
{
temp = temp.getNext();
}
temp.setNext(temp.getNext().getNext());
}
size--;
}
135) Which of the following piece of code has the functionality of counting the number of
elements in the list?
a)
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext();
}
return size;
}

b)
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
cur = cur.getNext();
size++;
}
return size;
}

c)
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext();
}
}

d)
public int length(Node head)
{
int size = 0;
Node cur = head;
while(cur!=null)
{
size++;
cur = cur.getNext().getNext();
}
return size;
}

136) Why do variable names beginning with the underscore is not encouraged?
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system

Answer: c

137). What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 2;
5. int b = 0;
6. int y = (b == 0) ? a :(a > b) ? (b = 1): a;
7. printf("%d\n", y);
8. }

a) Compile time error


b) 1
c) 2
d) Undefined behaviour

Answer: c
Explanation: None.

138). What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a = 2;
5. int b = 0;
6. int y = (b == 0) ? a :(a > b) ? (b = 1): a;
7. printf("%d\n", y);
8. }

a) Compile time error


b) 1
c) 2
d) Undefined behaviour

Answer: c
Explanation: None.

139) Comment on the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int a[5] = {1, 2, 3, 4, 5};
5. int i;
6. for (i = 0; i < 5; i++)
7. if ((char)a[i] == '5')
8. printf("%d\n", a[i]);
9. else
10. printf("FAIL\n");
11. }

a) The compiler will flag an error


b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times

Answer: d

140). What is short int in C programming?


a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic datatype
d) All of the mentioned

Answer: c

141) What is the output of the following C code?


1. #include <stdio.h>
2. int main()
3. {
4. signed char chr;
5. chr = 128;
6. printf("%d\n", chr);
7. return 0;
8. }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned

Answer: b.

142) Comment on the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. float x = 0.1;
5. printf("%d, ", x);
6. printf("%f", x);
7. }

a) 0.100000, junk value


b) Junk value, 0.100000
c) 0, 0.100000
d) 0, 0.999999

Answer: b
143) Which of the following statement is false?
a) A variable defined once can be defined again with different scope
b) A single variable cannot be defined with two different types in the same scope
c) A variable must be declared and defined at the same time
d) A variable refers to a location in memory

Answer: c

144) What is the output of this C code(when 1 is entered)?


1. #include <stdio.h>
2. void main()
3. {
4. double ch;
5. printf("enter a value between 1 to 2:");
6. scanf("%lf", &ch);
7. switch (ch)
8. {
9. case 1:
10. printf("1");
11. break;
12. case 2:
13. printf("2");
14. break;
15. }
16. }

a) Compile time error


b) 1
c) 2
d) Varies

Answer: a

145). What is the output of this C code?


1. #include <stdio.h>
2. int main()
3. {
4. int x = 97;
5. switch (x)
6. {
7. case 'a':
8. printf("yes ");
9. break;
10. case 97:
11. printf("no\n");
12. break;
13. }
14. }

a) yes
b) yes no
c) Duplicate case value error
d) Character case value error

Answer: c

146). Comment on the following union declaration?


1. #include <stdio.h>
2. union temp
3. {
4. int a;
5. float b;
6. char c;
7. };
8. union temp s = {1,2.5,'A'}; //REF LINE

Which member of the union will be active after REF LINE?


a) a
b) b
c) c
d) Such declaration are illegal

Answer: a.

147). Comment on the output of this C code?


1. #include <stdio.h>
2. struct temp
3. {
4. int a;
5. int b;
6. int c;
7. };
8. main()
9. {
10. struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
11. }

a) No Compile time error, generates an array of structure of size 3


b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure

Answer: a

148). What is the correct syntax to declare a function foo() which receives an array of structure
in function?
a) void foo(struct *var);
b) void foo(struct *var[]);
c) void foo(struct var);
d) none of the mentioned

Answer: a

149) What is the output of this C code?


1. #include <stdio.h>
2. struct student
3. {
4. char *name;
5. };
6. void main()
7. {
8. struct student s[2], r[2];
9. s[1] = s[0] = "alan";
10. printf("%s%s", s[0].name, s[1].name);
11. }

a) alan alan
b) Nothing
c) Compile time error
d) Varies

Answer: c

150) If the conversion characters of int d, i, o, u and x are preceded by h, it indicates?


a) A pointer to int
b) A pointer to short
c) A pointer to long
d) A pointer to char

Answer: b

151). What is the output of this C code?


1. #include <stdio.h>
2. void main()
3. {
4. char *s= "hello";
5. char *p = s;
6. printf("%c\t%c", p[0], s[1]);
7. }

a) Run time error


b) h h
c) h e
d) h l

Answer: c

152) What is the output of this C code?


1. #include <stdio.h>
2. int main(int argc, char** argv)
3. {
4. char *s = "myworld";
5. int i = 3;
6. printf("%10.*s", i, s);
7. }

a) myw
b) myworld(note:2 spaces before myworld)
c) myworld (note:2 spaces after myworld)
d) myw(note:6 spaces after myworld)

Answer: d

153) What is the difference between %e and %g?


a) %e output formatting depends on the argument and %g always formats in the format
[-]m.dddddd or [-]m.dddddE[+|-]xx where no.of ds are optional.
b) %e always formats in the format [-]m.dddddd or [-]m.dddddE[+|-]xx where no.of ds are
optional and output formatting depends on the argument.
c) No differences
d) Depends on the standard

Answer: b

154)

#include &lt;stdio.h&gt;
int main()
{
printf(&quot;\new_c_question\by&quot;);
printf(&quot;\rgeeksforgeeks&quot;);
getchar();
return 0;
}
(A)ew_c_question
geeksforgeeks
(B)new_c_ques
geeksforgeeks
(C)
geeksforgeeks
(D)Dependsonterminalconfiguration
Answer: (D)
155)

#include <stdio.h>

int main()
{
printf(" \"GEEKS %% FOR %% GEEKS\"");
getchar();
return 0;
}
Run on IDE

(A)“GEEKS % FOR % GEEKS”


(B) GEEKS % FOR % GEEKS
(C) \”GEEKS %% FOR %% GEEKS\”
(D) GEEKS %% FOR %% GEEKS
Answer: (A)

156)
#include <stdio.h>

int main()
{
printf("%c ", 5["GeeksQuiz"]);
return 0;
}
(A)Compile-timeerror
(B)Runtimeerror
(C)Q
(D)s
Answer: (C)

157)

#include <stdio.h>
int main()
{
printf("%c ", "GeeksQuiz"[5]);
return 0;
}
(A)Compile-timeerror
(B)Runtimeerror
(C)Q
(D)s
Answer:(C)

158)

Which of the following is true


(A) gets() can read a string with newline chacters but a normal scanf() with %s can not.
(B) gets() can read a string with spaces but a normal scanf() with %s can not.
(C) gets() can always replace scanf() without any additional code.
(D) None of the above
Answer: (B)
.

159)

A) gets() doesn’t do any array bound testing and should not be used.
(B) fgets() should be used in place of gets() only for files, otherwise gets() is fine
(C) gets() cannot read strings with spaces
(D) None of the above
Answer: (A)

160)

#include <stdio.h>
int main(void)
{
int x = printf("GeeksQuiz");
printf("%d", x);
return 0;
}
Run on IDE
(A)GeeksQuiz9
(B)GeeksQuiz10
(C)GeeksQuizGeeksQuiz
(D)GeeksQuiz1
Answer:(A)
161)
What is the return type of getchar()?
(A)int
(B)char
(C)unsignedchar
(D)float
Answer:(A)
162)Normally user programs are prevented from handling I/O directly by I/O instructions in
them. For CPUs having explicit I/O instructions, such I/O protection is ensured by having the I/O
instructions privileged. In a CPU with memory mapped I/O, there is no explicit I/O instruction.
Which one of the following is true for a CPU with memory mapped I/O?
(A) I/O protection is ensured by operating system routine (s)
(B) I/O protection is ensured by a hardware trap
(C) I/O protection is ensured during system configuration
(D) I/O protection is not possible
Answer: (A)

163)
#include "stdio.h"
int main()
{
int a = 10;
int b = 15;

printf("=%d",(a+1),(b=a+2));
printf(" %d=",b);

return 0;
}
(A)=11 15=
(B)=11 12=
(C)Compiler Error due to (b=a+2) in the first printf().
(D) No compile error but output would be =11 X= where X would depend on compiler
implementation.
Answer:(B)

164)
#include "stdio.h"

int foo(int a)
{
printf("%d",a);
return 0;
}

int main()
{
foo;
return 0;
}
Which of the following option is correct?
(A) It’ll result in compile error because foo is used without parentheses.
(B) No compile error and some garbage value would be passed to foo function. This would make
foo to be executed with output “garbage integer”.
(C) No compile error but foo function wouldn’t be executed. The program wouldn’t print
anything.
(D) No compile error and ZERO (i.e. 0) would be passed to foo function. This would make foo
to be executed with output 0.
Answer:(C)
165)
#include <stdio.h>
struct Ournode {
char x, y, z;
};

int main() {
struct Ournode p = {'1', '0', 'a' + 2};
struct Ournode *q = &p;
printf("%c, %c", *((char *)q + 1), *((char *)q + 2));
return 0;
}
The output of this program is:

(A)0,c
(B)0,a+2
(C)‘0’,‘a+2’
(D)‘0’,‘c’

Answer:(A)
Explanation: ‘a’ + 2 will be ‘c’, so Ournode p = {‘1’, ‘0’, ‘c’} and output will be 0, c.

166)
#include <stdio.h>
void fun1(char *s1, char *s2) {
char *temp;
temp = s1;
s1 = s2;
s2 = temp;
}
void fun2(char **s1, char **s2) {
char *temp;
temp = *s1;
*s1 = *s2;
*s2 = temp;
}
int main() {
char *str1 = "Hi", *str2 = "Bye";
fun1(str1, str2);
printf("%s %s", str1, str2);
fun2(&str1, &str2);
printf("%s %s", str1, str2);
return 0;
}
The output of the program above is

(A) Hi Bye Bye Hi


(B) Hi Bye Hi Bye
(C) Bye Hi Hi Bye
(D) Bye Hi Bye Hi
Answer:(A)

You might also like