Output of C++ programs | Set 35
Last Updated :
12 Jul, 2017
1. What will be the output of following program?
#include<iostream>
using namespace std;
int main()
{
int x = 5;
if (x==5)
{
if (x==5) break ;
cout<< "Hello" ;
}
cout<< "Hi" ;
}
|
Options
A) Compile error
B) Hi
C) HelloHi
D) Hello
Answer : A
Explanation: Compile error, keyword break can appear only within loop/switch statement.
2. What will be the output of following program?
#include<iostream>
using namespace std;
int main()
{
class student {
int rno = 10;
} v;
cout<<v.rno;
}
|
Options
A) 10
B) Garbage
C) Runtime error
D) Compile error
Answer : D
Explanation:Class member variables cannot be initialized directly. You can use member function to do such initialization.
3. What will be the output of following program?
‘cin’ is an __
Options
A) Class
B) Object
C) Package
D) Namespace
Answer : B
Explanation: It’s an object of istream class.
4. What will be the output of following program?
#include <iostream>
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;
}
|
Options
A) 6553
B) 6533
C) 6522
D) 12200
Answer : B
Explanation: In this program, we are adding the every element of two arrays. Finally we got output as 6533. Means for first array total sum will be 1200 + 200 + 2300 + 1230 + 1543 = 6473 and for second loop sum will be 12 14 + 16 + 18 = 60 (except 20 because loop is from 0 to 3 index) so result = 6473 + 60 = 6533.
5. Which rule will not affect the friend function?
Options
A) private and protected members of a class cannot be accessed from outside
B) private and protected member can be accessed anywhere
C) both a & b
D) None of the mentioned
Answer : A
Explanation: Friend is used to access private and protected members of a class from outside the same class.
6.What will be the output of following 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;
}
|
Options
A)8.31416
B)8
C)9
D)compile time error
Answer : B
Explanation: We are getting two variables from namespace variable and we are adding that with the help of scope resolution operator.The values are added to variable “a” which is of type int and thus the output is of type integer.
7.What will be the output of 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" ;
}
}
|
Options
A) x is greater
B) y is greater
C) Implementation defined
D) Arbitrary
Answer : A
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.
Share your thoughts in the comments
Please Login to comment...