0% found this document useful (0 votes)
414 views100 pages

Problem With Solution

This program defines an integer variable and prints its size. The output is 4, as integers typically occupy 4 bytes (32 bits) of memory on 32-bit systems.

Uploaded by

Tom Knob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
414 views100 pages

Problem With Solution

This program defines an integer variable and prints its size. The output is 4, as integers typically occupy 4 bytes (32 bits) of memory on 32-bit systems.

Uploaded by

Tom Knob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 100

1. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, b = 10, c = 15;

int *arr[ ] = {&a, &b, &c};

cout << arr[1];

return 0;

Answer: it will return some random number


Explanation: Array element cannot be address of auto variable. It can be address of static or
extern variables.

2. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};

cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];

return 0;

}
Answer: 21 21 21
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:

$ g++ point.cpp

$ a.out

21 21 21

3. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;

char *arr[] = {"C", "C++", "Java", "VBA"};

char *(*ptr)[4] = &arr;

cout << ++(*ptr)[2];

return 0;

Answer: ava
Explanation: In this program we are moving the pointer from first position to second
position and printing the remaining value.
Output:

$ g++ point1.cpp

$ a.out

Ava

5. What is the output of this program?


#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *p;

return 0;

Answer: 5
Explanation: In this program, we are making the pointer point to next value and printing
it.

$ g++ point3.cpp

$ a.out

6. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << arr;

return 0;

}
Answer: c) address of arr
Explanation: As we counted to print only arr, it will print the address of the array.
Output:

$ g++ point2.cpp

$ a.out

0xbfb1cff

7. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

int numbers[5];

int * p;

p = numbers; *p = 10;

p++; *p = 20;

p = &numbers[2]; *p = 30;

p = numbers + 3; *p = 40;

p = numbers; *(p + 4) = 50;

for (int n = 0; n < 5; n++)

cout << numbers[n] << ",";

return 0;

Answer: 10,20,30,40,50,
Explanation: In this program, we are just assigning a value to the array and printing it
and immediately dereferencing it.
Output:

$ g++ point4.cpp

$ a.out

10,20,30,40,50,

8. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *arr + 9;

return 0;

Answer: 13
Explanation: In this program, we are adding the value 9 to the initial value of the array,
So it’s printing as 13.
Output:

$ g++ point5.cpp

$ a.out

13

9. What is the output of the following program?

#include <iostream>
using namespace std;

int f(int p, int q)

if (p > q)

return p;

else

return q;

main()

int a = 5, b = 10;

int k;

bool x = true;

bool y = f(a, b);

k =((a * b) + (x + y));

cout << k;

Answer: 52

10. What is the value of p?

#include <iostream>

using namespace std;

int main()

{
int p;

bool a = true;

bool b = false;

int x = 10;

int y = 5;

p = ((x | y) + (a + b));

cout << p;

return 0;

Answer: 16

11. What will be the output of this program?

#include <iostream>

using namespace std;

int main()

char c = 74;

cout << c;

return 0;

Answer: J
Explanation: The literal value for 74 is J. So it will be printing J.

12. What is the output of this program?


#include <stdio.h>

int main()

char a = '\012';

printf("%d", a);

return 0;

Answer: 10
Explanation: The value ‘\012’ means the character with value 12 in octal, which is
decimal 10.

13. What is the output of the following program?

#include <iostream>

using namespace std;

int main()

int x = -1;

unsigned int y = 2;

if(x > y)

cout << "x is greater";

}
else

cout << "y is greater";

Answer: x is greater
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits
set, making it the bigger one.

14. What is the output of this program?

#include <iostream>

using namespace std;

int main()

float num1 = 1.1;

double num2 = 1.1;

if (num1 == num2)

cout << "stanford";

else

cout << "harvard";

return 0;

Answer: harvard
Explanation: Float store floating point numbers with 8 place accuracy and requires 4
bytes of Memory. Double has 16 place accuracy having the size of 8 bytes.
Output:
$ g++ float3.cpp

$ a.out

harvard

15. What is the output of this program?

#include <iomanip>

#include <iostream>

using namespace std;

int main()

cout << setprecision(17);

double d = 0.1;

cout << d << endl;

return 0;

Answer: b) 0.10000000000000001
Explantion: The double had to truncate the approximatioit’sue to its limited memory,
which resulted in a number that is not exactly 0.1.
Output:

$ g++ float2.out

$ a.out

0.10000000000000001
16. What is the output of the following program?

#include <iostream>

using namespace std;

int main()

float i = 123.0f;

cout << i << endl;

return 0;

Answer: 123
Explanation: The value 123 is printed because of its precision.

$ g++ float.cpp

$ a.out

123

17. What is the output of this program?

#include <iostream>

using namespace std;

int main()

float f1 = 0.5;

double f2 = 0.5;

if (f1 == 0.5f)

cout << "equal";

else
cout << "not equal";

return 0;

Answer: equal
Explanation: 0.5f results in 0.5 to be stored in floating point representations.
Output:

$ g++ float.cpp

$ a.out

equal

18. What is the output of the following program?

#include <iostream>

using namespace std;

int main()

int num = 0x20 + 020 + 20;

cout << sizeof(num)<<'\n';

return 0;

Answer: Depends on compiler


Explanation: The sum of three numbers are belongs to different number systems, so the
result is type casted into integer.
Output:

$ g++ size.cpp

$ a.out
4

19. What is the output of the following program?

#include <iostream>

using namespace std;

int main ( )

static double i;

i = 20;

cout << sizeof(i);

return 0;

Answer: 8
Explanation: The size of the double data type is 8.

$ g++ size1.cpp

$ a.out

20. What is the output of the following program?

#include <iostream>

using namespace std;

int main()

int num1 = 10;

float num2 = 20;

cout << sizeof(num1 + num2);


return 0;

Answer: 4
Explanation: In this program, integer is converted into float. Therefore the result of
num1 and num2 is float. And it is returning the size of the float.
Output:

$ g++ size2.cpp

$ a.out

21. What is the output of the following program?

#include <iostream>

using namespace std;

int main()

int a = 5;

float b;

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

cout << a;

return 0;

Answer: 4 5
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:

$ g++ size3.cpp

$ a.out
45

22. What would be the output of the following program (in 32-bit systems)?

#include <iostream>

using namespace std;

int main()

cout << sizeof(char);

cout << sizeof(int);

cout << sizeof(float);

return 0;

Answer: 1 4 4
Explanation: Character is 1 byte, integer 4 bytes and float 4 bytes.

23. What is the output of this program?

#include <iostream>

using namespace std;

enum cat

temp = 7

};

int main()

int age = 14;


age /= temp;

cout << "If you were cat, you would be " << age << endl;

return 0;

Answer: If you were cat, you would be 2


Explanation: The age will be divided by using compound assignment operator and so it
will return the age of the cat according to your age.

$ g++ enum1.cpp

$ a.out

If you were cat, you would be 2

24. What is the output of this program?

#include <iostream>

using namespace std;

enum test

A = 32, B, C

};

int main()

cout << A << B<< C;

return 0;

Answer: 323334
Explanation: If we not assigned any value to enum variable means, then the next
number to initialized number will be allocated to the variable.
Output:

$ g++ enum2.cpp

$ a.out

323334

25. What is the output of this program?

#include <iostream>

using namespace std;

enum colour {

green, red, blue, white, yellow, pink

};

int main()

cout << green<< red<< blue<< white<< yellow<< pink;

return 0;

Answer: 012345
Explanation: The enumerator values start from zero if it is unassigned.
Output:

$ g++ enum3.cpp

$ a.out

012345 <pre>[/expand]

26. What is the output of this program?

<pre lang="cpp" line="1" cssfile="hk1_style">


#include <iostream>

using namespace std;

int main()

enum channel {star, sony, zee};

enum symbol {hash, star};

int i = 0;

for (i = star; i <= zee; i++) {

printf("%d ", i);

return 0;

View Answer Answer: compile time error


Explanation: Enumeration variable ‘star’ appears two times in main() which causes the
error. An enumaration constant must be unique within the scope.

27. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;

enum month {

JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
};

for (i = MAR; i <= NOV; i++)

cout << i;

return 0;

a) 01234567891011
b) 123456789101112
c) 34567891011
d) 123456789
View Answer

Answer: c
Explanation: We are getting the values from march to november and printing its
concern number.

28. What is the output of this program?

#include <iostream>

using namespace std;

int g = 100;

int main()

int a;

int b;

b = 20;

a = 35;

g = 65;
cout << b << a << g;

a = 50;

cout << a << g;

return 0;

Answer: 2035655065
Explanation: The local values of a and g within the block are more dominant than the
global values.
Output:
$ g++ dec1.cpp
$ a.out
2035655065

29. What is the output of this program?

#include <iostream>

using namespace std;

void addprint()

static int s = 1;

s++;

cout << s;

int main()

addprint();

addprint();
addprint();

return 0;

Answer: 234
Explanation: The variable that is declared as static has a file scope.
Output:

advertisement

$ g++ dec2.cpp

$ a.out

234

30. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 10;

if (a < 10) {

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

cout << i;

else {

cout << i;

return 0;

}
Answer: error
Explanation: We will get compilation error because ‘i’ is an undeclared identifier.

31. What is the output of this program?

#include <iostream>

using namespace std;

int main()

char arr[20];

int i;

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

*(arr + i) = 65 + i;

*(arr + i) = '\0';

cout << arr;

return(0);

Answer: ABCDEFGHIJ

Explanation: Each time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned.


So it will print from A to J.
$ g++ point1.cpp
$ a.out
ABCDEFGHIJ

32. What is the output of this program?

#include <iostream>

using namespace std;

int main()

{
char *ptr;

char Str[] = "abcdefg";

ptr = Str;

ptr += 5;

cout << ptr;

return 0;

Answer: fg
Explanation: Pointer ptr points to string ‘fg’. So it prints fg.
Output:

$ g++ point.cpp

$ a.out

fg

33. What will be the output of this program?

#include <stdio.h>

using namespace std;

int array1[] = {1200, 200, 2300, 1230, 1543};

int array2[] = {12, 14, 16, 18, 20};

int temp, result = 0;

int main()

for (temp = 0; temp < 5; temp++)

result += array1[temp];
}

for (temp = 0; temp < 4; temp++)

result += array2[temp];

cout << result;

return 0;

Answer: 6533
Explanation: In this program we are adding the every element of two arrays. Finally we
got output as 6533.
Output:

$ g++ array.cpp

$ a.out

6533

34. What will be the output of the this program?

#include <stdio.h>

using namespace std;

int main ()

int array[] = {0, 2, 4, 6, 7, 5, 3};

int n, result = 0;

for (n = 0; n < 8; n++) {

result += array[n];
}

cout << result;

return 0;

Answer: None of the mentioned


Explanation: We are adding all the elements in the array and printing it. Total elements
in the array is 7, but our for loop will go beyond 7 and add a garbage value.

35. What is the output of this program?

#include <stdio.h>

using namespace std;

int main()

int a = 5, b = 10, c = 15;

int arr[3] = {&a, &b, &c};

cout << *arr[*arr[1] - 8];

return 0;

Answer: compile time error


Explanation: The conversion is invalid in this array. So it will arise error. The following
compilation error will be raised:
cannot convert from ‘int *’ to ‘int’

36. What is the output of this program?

#include <stdio.h>

using namespace std;


int main()

char str[5] = "ABC";

cout << str[3];

cout << str;

return 0;

Answer: ABC
Explanation: We are just printing the values of first 3 values.

$ g++ array.cpp

$ a.out

ABC

37. What is the output of this program?

#include <stdio.h>

using namespace std;

int main()

int array[] = {10, 20, 30};

cout << -2[array];

return 0;

Answer: -30
Explanation: It’s just printing the negative value of the concern element.

$ g++ array.cpp
$ a.out

-30

38. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};

cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];

return 0;

Answer: 21 21 21
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:

$ g++ point.cpp

$ a.out

21 21 21

39. What is the output of this program?

#include <iostream>

using namespace std;

int main()
{

int i;

char *arr[] = {"C", "C++", "Java", "VBA"};

char *(*ptr)[4] = &arr;

cout << ++(*ptr)[2];

return 0;

Answer: ava
Explanation: In this program we are moving the pointer from first position to second
position and printing the remaining value.
Output:

$ g++ point1.cpp

$ a.out

ava

40. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *p;

return 0;

}
Answer: 5
Explanation: In this program, we are making the pointer point to next value and printing
it.

$ g++ point3.cpp

$ a.out

41. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << arr;

return 0;

Answer: address of arr


Explanation: As we counted to print only arr, it will print the address of the array.
Output:

advertisement

$ g++ point2.cpp

$ a.out

0xbfb1cff

42. What is the output of this program?


#include <iostream>

using namespace std;

int main ()

int numbers[5];

int * p;

p = numbers; *p = 10;

p++; *p = 20;

p = &numbers[2]; *p = 30;

p = numbers + 3; *p = 40;

p = numbers; *(p + 4) = 50;

for (int n = 0; n < 5; n++)

cout << numbers[n] << ",";

return 0;

Answer: 10,20,30,40,50,
Explanation: In this program, we are just assigning a value to the array and printing it
and immediately dereferencing it.
Output:

$ g++ point4.cpp

$ a.out

10,20,30,40,50,

43. What is the output of this program?

#include <iostream>
using namespace std;

int main()

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *arr + 9;

return 0;

Answer: 13
Explanation: In this program, we are adding the value 9 to the initial value of the array,
So it’s printing as 13.
Output:

$ g++ point5.cpp

$ a.out

13

44. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int const p = 5;

cout << ++p;

return 0;

}
Answer: Error
Explanation: We cannot modify a constant integer value.

45. What is the output of this program?

#include <iostream>

using namespace std;

#define PI 3.14159

int main ()

float r = 2;

float circle;

circle = 2 * PI * r;

cout << circle;

return 0;

Answer: 12.5664
Explanation: In this program, we are finding the area of the circle by using concern
formula.
Output:

$ g++ cons.cpp

$ a.out

12.5664

46. What is the output of this program?

#include <iostream>

using namespace std;

void swap(int &a, int &b);


int main()

int a = 5, b = 10;

swap(a, b);

cout << "In main " << a << b;

return 0;

void swap(int &a, int &b)

int temp;

temp = a;

a = b;

b = temp;

cout << "In swap " << a << b;

Answer: In swap 105 In main 105


Explanation: As we are calling by reference the values in the address also changed. So
the main and swap values also changed.
Output:

$ g++ ref.cpp

$ a.out

In swap 105 In main 105


47. What is the output of this program?

#include <iostream>

using namespace std;

void swap(int &a, int &b);

int main()

int a = 5, b = 10;

swap(a, b);

cout << "In main " << a << b;

return 0;

void swap(int &a, int &b)

int temp;

temp = a;

a = b;

b = temp;

cout << "In swap " << a << b;

Answer: In swap 105 In main 105


Explanation: As we are calling by reference the values in the address also changed. So the main
and swap values also changed.
Output:

$ g++ ref.cpp

$ a.out
In swap 105 In main 105

48. What is the output of this program?

#include <iostream>

using namespace std;

void swap(int &a, int &b);

int main()

int a = 5, b = 10;

swap(a, b);

cout << "In main " << a << b;

return 0;

void swap(int &a, int &b)

int temp;

temp = a;

a = b;

b = temp;

cout << "In swap " << a << b;

Answer: In swap 105 In main 105


Explanation: As we are calling by reference the values in the address also changed. So
the main and swap values also changed.
Output:
$ g++ ref.cpp

$ a.out

In swap 105 In main 105

49. What is the output of this program?

#include <iostream>

using namespace std;

int func(void *Ptr);

int main()

char *Str = "abcdefghij";

func(Str);

return 0;

int func(void *Ptr)

cout << Ptr;

return 0;

Answer: address of string “abcdefghij”


Explanation: Even though it is a void pointer, we gets the address.
Output:

$ g++ b.cpp

$ a.out

0x8048714
50. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int *p;

void *vp;

if (vp == p);

cout << "equal";

return 0;

Answer: equal
Explanation: The void pointer is easily converted to any other type of pointer, so these
are equal.
Output:

$ g++ poi4.cpp

$ a.out

Equal

51. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;
char c;

void *data;

i = 2;

c = 'd';

data = &i;

cout << "the data points to the integer value" << data;

data = &c;

cout << "the data now points to the character" << data;

return 0;

Answer: two memory addresses


Explanation: Because the data points to the address value of the variables only, So it is
printing the memory address of these two variable.
Output:

$ g++ poi2.cpp

$ a.out

the data points to the integer value0xbfc81824 the data now points to the
character0xbfc8182f

52. What is the output of this program?

advertisement

#include <iostream>

using namespace std;

int main()

int n = 5;
void *p = &n;

int *pi = static_cast<int*>(p);

cout << *pi << endl;

return 0;

Answer: 5
Explanation: We just casted this from void to int, so it prints 5
Output:

$ g++ poi1.cpp

$ a.out

53. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, c;

void *p = &a;

double b = 3.14;

p = &b;

c = a + b;

cout << c << '\n' << p;

return 0;

}
Answer: 8, memory address
Explanation: In this program, we are just adding the two values and printing it.
Output:

$ g++ poi.cpp

$ a.out

0xbfef0378

54. What is the output of this program?

#include <iostream>

#include <string.h>

using namespace std;

int main()

struct student

int num;

char name[25];

};

student stu;

stu.num = 123;

strcpy(stu.name, "John");

cout << stu.num << endl;

cout << stu.name << endl;

return 0;
}

Answer: 123
Explanation: We are copying the value john to the name and then we are printing the
values that are in the program.
Output:

$ g++ stu.cpp

$ a.out

123

John

55. What is the output of this program?

#include <iostream>

using namespace std;

struct Time

int hours;

int minutes;

int seconds;

};

int toSeconds(Time now);

int main()

Time t;

t.hours = 5;

t.minutes = 30;
t.seconds = 45;

cout << "Total seconds: " << toSeconds(t) << endl;

return 0;

int toSeconds(Time now)

return 3600 * now.hours + 60 * now.minutes + now.seconds;

Answer: 19845
Explanation: In this program, we are just converting the given hours and minutes into
seconds.
Output:

$ g++ stu1.cpp

$ a.out

Total seconds:19845

56. What will be the output of this program?

#include <iostream>

using namespace std;

int main()

struct ShoeType

string style;

double price;
};

ShoeType shoe1, shoe2;

shoe1.style = "Adidas";

shoe1.price = 9.99;

cout << shoe1.style << " $ "<< shoe1.price;

shoe2 = shoe1;

shoe2.price = shoe2.price / 9;

cout << shoe2.style << " $ "<< shoe2.price;

return 0;

Answer: Adidas $ 9.99


Adidas $ 1.11
Explanation: We copied the value of shoe1 into shoe2 and divide the shoe2 value
by 9, So this is the output.
Output:

$ g++ stu2.cpp

$ a.out

Adidas $ 9.99

Adidas $ 1.11

57. What is the output of this program?

#include <iostream>

using namespace std;

struct sec

int a;
char b;

};

int main()

struct sec s ={25,50};

struct sec *ps =(struct sec *)&s;

cout << ps->a << ps->b;

return 0;

Answer: 252
Explanation: In this program, We are dividing the values of a and b, printing it.
Output:

$ g++ stu5.cpp

$ a.out

252

58. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a;

a = 5 + 3 * 5;

cout << a;

return 0;
}

Answer: 20
Explanation: Because the * operator is having highest precedence, So it is executed first
and then the + operator will be executed.
Output:

$ g++ op1.cpp

$ a.out

20

59. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, b = 6, c, d;

c = a, b;

d = (a, b);

cout << c << ' ' << d;

return 0;

Answer: 5 6
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:

$ g++ op3.cpp

$ a.out

5 6
60. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i, j;

j = 10;

i = (j++, j + 100, 999 + j);

cout << i;

return 0;

Answer: 1010
Explanation: j starts with the value 10. j is then incremented to 11. Next, j is added to
100. Finally, j (still containing 11) is added to 999 which yields the result 1010.
Output:

$ g++ op2.cpp

$ a.out

1010

61. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

int x, y;
x = 5;

y = ++x * ++x;

cout << x << y;

x = 5;

y = x++ * ++x;

cout << x << y;

return 0;

Answer: 749736
Explanation: Because of the precedence the pre-increment and post increment
operator, we got the output as 749736.
Output:

$ g++ op.cpp

$ a.out

749736

62. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int a = 5, b = 6, c;

c = (a > b) ? a : b;

cout << c;

return 0;
}

Answer: 6
Explanation: Here the condition is false on conditional operator, so the b value is
assigned to c.
Output:

$ g++ op1.cpp

$ a.out

63. What is the output of this program?

#include <iostream>

using namespace std;

main()

double a = 21.09399;

float b = 10.20;

int c ,d;

c = (int) a;

d = (int) b;

cout << c <<' '<< d;

return 0;

Answer: 21 10
Explanation: In this program, we are casting the operator to integer, So it is printing as
21 and 10.
Output:

$ g++ op5.cpp
$ a.out

21 10

64. What is the output of this program?

#include <iostream>

using namespace std;

int main ()

int n;

for (n = 5; n > 0; n--)

cout << n;

if (n == 3)

break;

return 0;

Answer: 543
Explanation: In this program, We are printing the numbers in reverse order but by using
break statement we stopped printing on 3.
Output:

$ g++ stat.cpp

$ a.out

543

65. What is the output of this program?


#include <iostream>

using namespace std;

int main()

int a = 10;

if (a < 15)

time:

cout << a;

goto time;

break;

return 0;

Answer: compile time error


Explanation: Because the break statement need to be presented inside a loop or a
switch statement.

66. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int n = 15;

for ( ; ;)
cout << n;

return 0;

a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
View Answer

Answer: c
Explanation: There is not a condition in the for loop, So it will loop continuously.

67. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int i;

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

cout << i;

return 0;

Answer: 10
Explanation: for loop with a semicolon is called as body less for loop. It is used only for
incrementing the variable values. So in this program the value is incremented and
printed as 10.
Output:

$ g++ stat2.cpp

$ a.out

10

68. What is the output of this program?

#include <iostream>

using namespace std;

int main()

/* this is comment*

cout << "hello world";

return 0;

Answer: compile time error


Explanation: Because the slash should need to be forward not backward.

69. What is the output of this program?

#include <iostream>

using namespace std;

long factorial (long a)

if (a > 1)

return (a * factorial (a + 1));


else

return (1);

int main ()

long num = 3;

cout << num << "! = " << factorial ( num );

return 0;

Answer: segmentation fault


Explanation: As we have given in the function as a+1, it will exceed the size and so it
arises the segmentation fault.
Output:

$ g++ arg3.cpp

$ a.out

segmentation fault

70. What is the output of this program?

#include <iostream>

using namespace std;

void square (int *x)

*x = (*x + 1) * (*x);

int main ( )
{

int num = 10;

square(&num);

cout << num;

return 0;

Answer: 110
Explanation: We have increased the x value in operand as x + 1, so it will return as 110.
Output:

$ g++ arg2.cpp

$ a.out

110

71. What is the output of this program?

#include <iostream>

using namespace std;

int add(int a, int b);

int main()

int i = 5, j = 6;

cout << add(i, j) << endl;

return 0;

int add(int a, int b )

{
int sum = a + b;

a = 7;

return a + b;

Answer: 13
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:

$ g++ arg1.cpp

$ a.out

13

72. What is the output of this program?

#include <iostream>

using namespace std;

void Sum(int a, int b, int & c)

a = b + c;

b = a + c;

c = a + b;

int main()

int x = 2, y =3;

Sum(x, y, y);

cout << x << " " << y;

return 0;
}

Answer: 2 15
Explanation: We have passed three values and it will manipulate according to the given
condition and yield the result as 2 15.
Output:

$ g++ arg.cpp

$ a.out

2 15

73. What is the output of this program?

#include <iostream>

using namespace std;

void mani()

void mani()

cout<<"hai";

int main()

mani();

return 0;

Answer: compile time error


Explanation: We have to use the semicolon to declare the function in line 3. If we did
means, the program will execute

.
74. What is the output of this program?

#include <iostream>

using namespace std;

void fun(int x, int y)

x = 20;

y = 10;

int main()

int x = 10;

fun(x, x);

cout << x;

return 0;

Answer: 10
Explanation: In this program, we called by value so the value will not be changed, So the
output is 10
Output:

$ g++ fun.cpp

$ a.out

10

75. What is the output of this program?

#include <iostream>
using namespace std;

void copy (int& a, int& b, int& c)

a *= 2;

b *= 2;

c *= 2;

int main ()

int x = 1, y = 3, z = 7;

copy (x, y, z);

cout << "x =" << x << ", y =" << y << ", z =" << z;

return 0;

Answer: 2 6 14
Explanation: Because we multiplied the values by 2 in the copy function.
Output:

$ g++ arg6.cpp

$ a.out

x = 2,y = 6,z = 14

76. What is the new value of x?

#include <iostream>

using namespace std;

void fun(int &x)


{

x = 20;

int main()

int x = 10;

fun(x);

cout << "New value of x is " << x;

return 0;

Answer: 20
Explanation: As we passed by reference, the value is changed and it is returned as 20.
Output:

$ g++ arg5.cpp

$ a.out

20

77. What is the output of this program?

#include <iostream>

using namespace std;

long factorial (long a)

if (a > 1)

return (a * factorial (a + 1));

else
return (1);

int main ()

long num = 3;

cout << num << "! = " << factorial ( num );

return 0;

Answer: segmentation fault


Explanation: As we have given in the function as a+1, it will exceed the size and so it arises the
segmentation fault.
Output:

$ g++ arg3.cpp

$ a.out

segmentation fault

78. What is the output of this program?

#include <iostream>

using namespace std;

void square (int *x)

*x = (*x + 1) * (*x);

int main ( )

{
int num = 10;

square(&num);

cout << num;

return 0;

Answer: 110
Explanation: We have increased the x value in operand as x+1, so it will return as 110.
Output:

advertisement

$ g++ arg2.cpp

$ a.out

110

79. What is the output of this program?

#include <iostream>

using namespace std;

int add(int a, int b);

int main()

int i = 5, j = 6;

cout << add(i, j) << endl;

return 0;

int add(int a, int b )

{
int sum = a + b;

a = 7;

return a + b;

Answer: 13
Explanation: The value of a has been changed to 7, So it returns as 13.
Output:

$ g++ arg1.cpp

$ a.out

13

80. What is the output of this program?

#include <iostream>

using namespace std;

void Sum(int a, int b, int & c)

a = b + c;

b = a + c;

c = a + b;

int main()

int x = 2, y =3;

Sum(x, y, y);

cout << x << " " << y;


return 0;

Answer: 2 15
Explanation: We have passed three values and it will manipulate according to the given
condition and yield the result as 2 15
Output:

$ g++ arg.cpp

$ a.out

2 15

81. What is the output of this program?

#include <iostream>

using namespace std;

int max(int a, int b )

return ( a > b ? a : b );

int main()

int i = 5;

int j = 7;

cout << max(i, j );

return 0;

}
Answer: 7
Explanation: In this program, we are returning the maximum value by using conditional
operator.
Output:

$ g++ ret.cpp

$ a.out

82. What is the output of this program?

#include <iostream>

using namespace std;

double & WeeklyHours()

double h = 46.50;

double &hours = h;

return hours;

int main()

double hours = WeeklyHours();

cout << "Weekly Hours: " << hours;

return 0;

Answer: 46.5
Explanation: We are returning the value what we get as input.
Output:

$ g++ ret1.cpp
$ a.out

46.5

83. What is the output of this program?

advertisement

#include <iostream>

using namespace std;

int mult (int x, int y)

int result;

result = 0;

while (y != 0)

result = result + x;

y = y - 1;

return(result);

int main ()

int x = 5, y = 5;

cout << mult(x, y) ;

return(0);

}
a) 20
b) 25
c) 30
d) 35
View Answer

Answer: b
Explanation: We are multiplying these values by adding every values.
Output:

$ g++ ret.cpp

$ a.out

25

84. What is the output of this program?

#include <iostream>

using namespace std;

int gcd (int a, int b)

int temp;

while (b != 0)

temp = a % b;

a = b;

b = temp;

return(a);

}
int main ()

int x = 15, y = 25;

cout << gcd(x, y);

return(0);

Answer: 5
Explanation: In this program, we are finding the gcd of the number.
Output:

$ g++ ret5.cpp

$ a.out

85. What is the output of this program?

#include <iostream>

using namespace std;

void print(int i)

cout << i;

void print(double f)

cout << f;

int main(void)
{

print(5);

print(500.263);

return 0;

a) 5500.263
b) 500.2635
c) 500.263
d) none of the mentioned
View Answer

Answer: a
Explanation: In this program, we are printing the values and the values will be print(5)
will be printed first because of the order of the execution.
Output:

$ g++ over.cpp

$ a.out

5500.263

86. What is the output of this program?

#include <iostream>

using namespace std;

int Add(int X, int Y, int Z)

return X + Y;

double Add(double X, double Y, double Z)

{
return X + Y;

int main()

cout << Add(5, 6);

cout << Add(5.5, 6.6);

return 0;

Answer: ) compile time error


Explanation: None.

87. What is the output of the following program?

#include <iostream>

using namespace std;

int operate (int a, int b)

return (a * b);

float operate (float a, float b)

return (a / b);

int main()

{
int x = 5, y = 2;

float n = 5.0, m = 2.0;

cout << operate(x, y) <<"\t";

cout << operate (n, m);

return 0;

Answer: 10 2.5
Explanation: In this program, we are divide and multiply the values.
Output:

advertisement

$ g++ over3.cpp

$ a.out

10 2.5

88. What is the output of this program?

#include <iostream>

using namespace std;

void func(int a, bool flag = true)

if (flag == true )

cout << "Flag is true. a = " << a;

else

{
cout << "Flag is false. a = " << a;

int main()

func(200, false);

return 0;

Answer: Flag is false. a = 200


Explanation: In this program, we are passing the value, as it evaluates to false, it
produces the output as following.
Output:

$ g++ def.cpp

$ a.out

Flag is false. a = 200

89. What is the output of this program?

#include <iostream>

#include <string>

using namespace std;

string askNumber(string prompt = "Please enter a number: ");

int main()

string number = askNumber();

cout << "Here is your number: " << number;


return 0;

string askNumber(string prompt)

string number;

cout << prompt;

cin >> number;

return number;

Answer: the number you entered


Explanation: In this program, we are getting a number and printing it.
Output:

$ g++ def1.cpp

$ a.out

Please enter a number:

Here is your number:5

90. What is the output of this program?

advertisement

#include <iostream>

using namespace std;

void Values(int n1, int n2 = 10)

using namespace std;


cout << "1st value: " << n1;

cout << "2nd value: " << n2;

int main()

Values(1);

Values(3, 4);

return 0;

a)
b) 1st value: 1
10
3
10
c) compile time error
d) none of the mentioned
View Answer

Answer: 1st value: 1


10
3
4
Explanation: In this program, We are passing the values as by default values rules it is working.
Output:

$ g++ def2.cpp

$ a.out

1st value: 1

2nd value: 10

1st value: 3
2nd value: 4

91. What is the output of this program?

#include <iostream>

using namespace std;

int func(int m = 10, int n)

int c;

c = m + n;

return c;

int main()

cout << func(5);

return 0;

Answer: compile time error


Explanation: We can’t use the user argument in front of the default argument.

92. What is the output of this program?

#include <iostream>

#include <stdarg.h>
using namespace std;

float avg( int Count, ... )

va_list Numbers;

va_start(Numbers, Count);

int Sum = 0;

for (int i = 0; i < Count; ++i )

Sum += va_arg(Numbers, int);

va_end(Numbers);

return (Sum/Count);

int main()

float Average = avg(10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

cout << "Average of first 10 whole numbers : " << Average;

return 0;

Answer: 4
Explanation: We are just calculating the average of these numbers using cstdarg.
Output:

$ g++ uka.cpp

$ a.out

Average of first 10 whole numbers 4


93. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

int add (int num, ...)

int sum = 0;

va_list args;

va_start (args,num);

for (int i = 0; i < num; i++)

int num = va_arg (args,int);

sum += num;

va_end (args);

return sum;

int main (void)

int total = add(8, 1, 2, -1, 4, 12, -2, 9, 7);

cout << "The result is " << total;

return 0;

}
a) 32
b) 23
c) 48
d) compile time error
View Answer

Answer: a
Explanation: We are adding these numbers by using for statement and stdarg.
Output:

$ g++ uka.cpp

$ a.out

The result is 32

94. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

void dumplist(int, ...);

int main()

dumplist(2, 4, 8);

dumplist(3, 6, 9, 7);

return 0;

void dumplist(int n, ...)

va_list p;

int i;
va_start(p, n);

while (n-->0)

i = va_arg(p, int);

cout << i;

va_end(p);

Answer: 48697
Explanation: In this program, we are eradicating the first value
by comparing using while operator.
Output:

$ g++ rka3.cpp

$ a.out

48697

95. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

int flue(char c,...);

int main()

int x, y;

x = flue('A', 1, 2, 3);
y = flue('1', 1.0,1, '1', 1.0f, 1l);

cout << x << y;

return 0;

int flue(char c,...)

return c;

Answer: 6549
Explanation: In this program, we are returning the ascii value of the character and
printing it.
Output:

$ g++ rka4.cpp

$ a.out

6549

96. What is the output of this program?

#include <iostream>

#include <stdarg.h>

using namespace std;

void fun(std::string msg, ...);

int main()

fun("IndiaBIX", 1, 4, 7, 11, 0);

return 0;
}

void fun(std::string msg, ...)

va_list ptr;

int num;

va_start(ptr, msg);

num = va_arg(ptr, int);

num = va_arg(ptr, int);

cout << num;

Answer: 4
Explanation: In this program, we are moving the pointer to the second value and
printing it.
Output:

$ g++ uka6.cpp

$ a.out

97. What is the output of this program?

#include <iostream>

using namespace std;

int add(int first, int second)

return first + second + 15;

}
int operation(int first, int second, int (*functocall)(int, int))

return (*functocall)(first, second);

int main()

int a;

int (*plus)(int, int) = add;

a = operation(15, 10, plus);

cout << a;

return 0;

Answer: 45
Explanation: In this program, we are adding two numbers with 15, So we got the output
as 40.
Output:

$ g++ pfu2.cpp

$ a.out

40

98. What is the output of this program?

#include <iostream>

using namespace std;

void func(int x)

{
cout << x ;

int main()

void (*n)(int);

n = &func;

(*n)( 2 );

n( 2 );

return 0;

Answer: 22
Explanation: As we are calling the function two times with the same value, So it is
printing as 22.
Output:

$ g++ pfu.cpp

$ a.out

22

99. What is the output of this program?

#include <iostream>

using namespace std;

int n(char, int);

int (*p) (char, int) = n;

int main()

{
(*p)('d', 9);

p(10, 9);

return 0;

int n(char c, int i)

cout << c << i;

return 0;

Answer: d9
Explanation: None.
Output:

$ g++ pfu1.cpp

$ a.out

d9

100. What is the output of this program?

#include <iostream>

using namespace std;

int func (int a, int b)

cout << a;

cout << b;

return 0;
}

int main(void)

int(*ptr)(char, int);

ptr = func;

func(2, 3);

ptr(2, 3);

return 0;

Answer: compile time error


Explanation: In this program, we can’t do the casting from char to int, So it is
raising an error.

101. What is the output of this program?

#include <iostream>

using namespace std;

int add(int first, int second)

return first + second + 15;

int operation(int first, int second, int (*functocall)(int, int))

return (*functocall)(first, second);

int main()
{

int a;

int (*plus)(int, int) = add;

a = operation(15, 10, plus);

cout << a;

return 0;

Answer: 40
Explanation: In this program, we are adding two numbers with 15, So we got the output
as 40.
Output:

$ g++ pfu2.cpp

$ a.out

40

102. What is the output of this program?

#include <iostream>

using namespace std;

void func(int x)

cout << x ;

int main()

void (*n)(int);
n = &func;

(*n)( 2 );

n( 2 );

return 0;

Answer: 22
Explanation: As we are calling the function two times with the same value, So it is
printing as 22.
Output:

$ g++ pfu.cpp

$ a.out

22

103. What is the output of this program?

#include <iostream>

using namespace std;

int n(char, int);

int (*p) (char, int) = n;

int main()

(*p)('d', 9);

p(10, 9);

return 0;

int n(char c, int i)


{

cout << c << i;

return 0;

Answer: d9
Explanation: None.
Output:

$ g++ pfu1.cpp

$ a.out

d9

104. What is the output of this program?

#include <iostream>

using namespace std;

int func (int a, int b)

cout << a;

cout << b;

return 0;

int main(void)

int(*ptr)(char, int);

ptr = func;

func(2, 3);
ptr(2, 3);

return 0;

Answer: compile time error


Explanation: In this program, we can’t do the casting from char to int, So it is raising an
error.

105. What is the output of this program?

#include <iostream>

using namespace std;

namespace first

int var = 5;

namespace second

double var = 3.1416;

int main ()

int a;

a = first::var + second::var;

cout << a;

return 0;
}

Answer: 8
Explanation: As we are getting two variables from namespace variable and we are
adding that.
Output:

$ g++ name.cpp

$ a.out

106. What is the output of this program?

#include <iostream>

using namespace std;

namespace first

int x = 5;

int y = 10;

namespace second

double x = 3.1416;

double y = 2.7183;

int main ()

using first::x;
using second::y;

bool a, b;

a = x > y;

b = first::y < second::x;

cout << a << b;

return 0;

Answer: 10
Explanation: We are inter mixing the variable and comparing it which is bigger and
smaller and according to that we are printing the output.
Output:

$ g++ name1.cpp

$ a.out

10

107. What is the output of this program?

#include <iostream>

using namespace std;

namespace Box1

int a = 4;

namespace Box2

int a = 13;
}

int main ()

int a = 16;

Box1::a;

Box2::a;

cout << a;

return 0;

Answer: 16
Explanation: In this program, as there is lot of variable a and it is printing the value
inside the block because it got the highest priority.
Output:

$ g++ name2.cpp

$ a.out

16

108. What is the output of this program?

#include <iostream>

using namespace std

namespace space

int x = 10;

namespace space
{

int y = 15;

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

space::x = space::y =5;

cout << space::x << space::y;

Answer: 55
Explanation: We are overriding the value at the main function and so we are getting the
output as 55.
Output:

$ g++ name4.cpp

$ a.out

109. What is the output of this program?

#include <iostream>

using namespace std;

namespace extra

int i;

void i()

{
using namespace extra;

int i;

i = 9;

cout << i;

int main()

enum letter { i, j};

class i { letter j; };

::i();

return 0;

Answer: 9
Explanation: A scope resolution operator without a scope qualifier refers to the global
namespace.

110. What is the output of this program?

#include <iostream>

using namespace std;

int main()

int age = 0;

try

if (age < 0)
{

throw "Positive Number Required";

cout << age;

catch(const char *Message)

cout << "Error: " << Message;

return 0;

Answer: 0
Explanation: As the zero marks the beginning of the positive number, it is printed as
output
Output:

$ g++ excep.cpp

$ a.out

111. What is the output of this program?

#include <iostream>

using namespace std;

void PrintSequence(int StopNum)

int Num;
Num = 1;

while (true)

if (Num >= StopNum)

throw Num;

cout << Num;

Num++;

int main(void)

try

PrintSequence(20);

catch(int ExNum)

cout << "Caught an exception with value: " << ExNum;

return 0;

Answer: prints first 19 numbers and throws exception at 20


Explanation: In this program, we are printing upto 19 numbers and when executing the
20, we are raising a exception.
Output:
$ g++ excep1.cpp

$ a.out

12345678910111213141516171819Caught an exception with value: 20

112. What is the output of this program?

#include <iostream>

using namespace std;

double division(int a, int b)

if (b == 0)

throw "Division by zero condition!";

return (a / b);

int main ()

int x = 50;

int y = 2;

double z = 0;

try

z = division(x, y);

cout << z;
}

catch(const char *msg)

cerr << msg;

return 0;

Answer: 35
Explanation: In this program, we resembling the division by using the exception
handling.
Output:

$ g++ excep2.cpp

$ a.out

25

113. What is the output of this program?

#include <iostream>

using namespace std;

int main()

char* buff;

try

buff = new char[1024];

if (buff == 0)
throw "Memory allocation failure!";

else

cout << sizeof(buff) << "Byte successfully allocated!"<<endl;

catch(char *strg)

cout<<"Exception raised: "<<strg<<endl;

return 0;

Answer: Depends on the size of the data type


Explanation: As we are allocating the memory to the variables and if there are not
sufficient size means, it will throw an exception.
Output:

$ g++ excep3.cpp

$ a.out

4 Bytes allocated successfully

8. What is the output of this program?

#include <iostream>

using namespace std;

void Funct();

int main()

try

{
Funct();

catch(double)

cerr << "caught a double type..." << endl;

return 0;

void Funct()

throw 3;

Answer: abnormal program termination


Explanation: As we are throwing integer to double it will raise as abnormal program
after termination throw statement.
Output:

$ g++ excep4.cpp

$ a.out

terminate called after throwing an instance of 'int'

Aborted

114. What is the output of this program?

#include <iostream>

#include <exception>

using namespace std;


int main()

try

int * array1 = new int[100000000];

int * array2 = new int[100000000];

int * array3 = new int[100000000];

int * array4 = new int[100000000];

cout << "Allocated successfully";

catch(bad_alloc&)

cout << "Error allocating the requested memory." << endl;

return 0;

Answer: Depends on the memory of the computer


Explanation: In this program, we allocating the memory to the arrays by using exception
handling and we handled the exception by standard exception.
Output:

$ g++ excep5.cpp

$ a.out

Allocated successfully

You might also like