Shantanu wants to make a program to print the sum of the first 7 multiples of 6. He writes the following program:
integer i = 0 // statement 1
integer sum // statement 2
while ( i <= 42 ) // statement 3
{
sum = sum + i // statement 4
i = i + 6;
}
print sum // statement 6
Does this program have an error? If yes, which one statement will you modify to correct the program?
Statement 1
Statement 2
Statement 3
Statement 4
Answer : Option B.
Which of the following statements is true after execution of the program?
int a[10],I,*p;
a[0] = 1;
a[1] = 2;
p = a;
(*p)++;
a[0] = 2
a[1] = 3
a[1] = 1
a[0] = 3
Answer : Option A.
Predict the output of following C++ program.
#include
using namespace std;
class abc
{
static int x;
public:
abc() { x++;}
static int getX() {return x;}
};
int Test::x = 0;
int main()
{
cout << abc::getX() << " ";
abc t[5];
cout << abc::getX(); }
55
55
05
50
Answer : Option C.
abc::getX()" is a static function so it can be called without object. Since x is initialized as 0, the first call to getX() returns 0. As x++ in constructor and when an array of 5 objects is created, the constructor is called 5 times. So x is incremented to 5 before the next call to getX().
What will be output of following program?
#include
int main()
{
int huge *a =(int huge *)0x59990005;
int huge *b =(int huge *)0x59980015;
if(a == b)
printf("power of pointer");
else
printf("power of c");
return 0;
}
power of pointer
power of c
power of c power of c
Compilation error
Answer : Option A.
Assume that the Point class is existing with the following snippet in the header file
Point.h:
class Point
{
Point(); Pont(int, int); int GetX();
int GetY();
void SetX(int);
void SetY(int);
};
If the objects of Point are created as Point oPointOne, oPointTwo(2,3); Which of the following statements are correct?
(i)The statement oPointOne.SetX(20);
Will compile and run successfully.
(ii) The statement oPointOne.SetX(20).SetY(30);
Will compile successfully but will give a run time error.