24 Jan 2014

Basic c++ practice enum structure array string and function

These are basic level question asked in various companies. This is practice paper for C++ Software professionals. I am planning to prepare middle and advance level questions collections in coming days. You are most welcome to give feedback for these questions in comments sections.  



Question 1 INFOSYS

#include <iostream>

    using namespace std;

    enum colour {

        green, red, blue, white, yellow, pink

    };

    int main()

    {

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

        return 0;

    }
EXplanation:The variable of the enumerator will be assigned zero if it is unassigned.
Output:
0123456

Question 2 TCS

#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;

    }

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

Question 3 WIPRO

#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;

    }

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

Question 4 TECH MAHINDRA

#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.

Question 5 SAMSUNG

#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);

    }


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.
Output
ABCDEFGHIJ

Question 6 HCL

#include <iostream>

    using namespace std;

    int main()

    {

        char *ptr;

        char Str[] = "abcdefg";

        ptr = Str;

        ptr += 5;

        cout << ptr;

        return 0;

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

Question 7 SIEMENS

#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;

    }


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

Question 8 HONEYWELL

#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 < 5 ;n++) {

            result += billy[n];

        }

        cout << result;

        return 0;

    }


Explanation:We are adding all the elements in the array and printing it.
Output:
27

Question 9 HCL

#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
Explantion:The conversion is invalid in this array. So it will arise error.

What is the output of this program?

Question 10 ABB

    #include <stdio.h>

    using namespace std;

    int main()

    {

        char str[5] = "ABC";

        cout << str[3];

        cout << str;

        return 0;

    }

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

ABC

Question 11 ACCENTURE

    #include <stdio.h>

    using namespace std;

    int main()

    {

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

        cout << -2[array];

        return 0;

    }


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

Question 12 ADP

   #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;

   }


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

Question 13 BEL

   #include <iostream>

   using namespace std;

   int main()

   {

       int i;

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

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

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

       return 0;

   }


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

Question 14 CDOT

   #include <iostream>

   using namespace std;

   int main()

   {

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

       int *p = (arr + 1);

       cout << *p;

       return 0;

   }

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

Question 15 CITRIX

   #include <iostream>

   using namespace std;

   int main()

   {

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

       int *p = (arr + 1);

       cout << arr;

       return 0;

   }


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

Question 16 CTS

   #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;

   }

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

Question 17 NIIT

   #include <iostream>

   using namespace std;

   int main()

   {

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

        int *p = (arr + 1);

        cout << *arr + 9;

        return 0;

   }

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

Question 18 ORACLE

    #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.

Question 19 ONMOBILE

    #include <iostream>

    using namespace std;

    #define PI 3.14159

    int main ()

    {

        float r = 2;

        float circle;

        circle = 2 * PI * r;

        cout << circle;

        return 0;

    }

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

Question 20 NEWGEN

    #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;

    }

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

Question 21 MCAFEE

    #include <iostream>

    using namespace std;

    int main()

    {

        int a = 9;

        int & aref = a;

        a++;

        cout << "The value of a is " << aref;

        return 0;

    }

Explanation:The value is declared and it is post incremented, so it’s value is 10.
Output

10

Question 22 MASTEK

    #include <iostream>

    using namespace std;

    void print (char * a)

    {

        cout << a << endl;

    }

    int main ()

    {

        const char * a = "Hello world";

        print(const_cast<char *> (a) );

        return 0;

    }

Explanation:In this program we used the concept of constant casting to cast the variable and printing it.
Output:
Hello world

Question 23 L&T INFOTECH

    #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: compile time error
Explanation:We cannot present the void value as argument in the function.

Question 24 KMG

    #include <iostream>

    using namespace std;

    int main()

    {

        int *p;

        void *vp;

        if (vp == p);

            cout << "equal";

        return 0;

    }

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

Question 25 KPIT

    #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;

    }


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:
the data points to the integer value0xbfc81824 the data now points to the character0xbfc8182f

Question 26 LG SOFT

    #include <iostream>

    using namespace std;

    int main()

    {

        int n = 5;

        void *p = &n;

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

        cout << *pi << endl;

        return 0;

    }


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

Question 27 AMDOCS

    #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;

    }


Explanation: In this program, we are just adding the two values and printing it.
Output:
8
0xbfef0378

Question 28 MASTEC

    #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;

    }

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

Question 29 MINDTREE

    #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;

    }


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

Question 30 BPCL

    #include <iostream>

    using namespace std;

    int main()

    {

        struct ShoeType {

            char 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;

    }

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

Question 31 AVAYA

    #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;

    }

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

Question 32 ACCENTURE

    #include <iostream>

    using namespace std;

    void square (int *x)

    {

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

    }

    int main ( )

    {

       int num = 10;

        square(&num);

       cout << num;

       return 0;

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

Question 33  CIENNA

    #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;

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

Question 34 CAPGEMINI

    #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;

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

Question 35 CANON INDIA

    #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.

Question 36 BIRLA SOFT

    #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;

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

Question 37 AGILENT

    #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;

    }
Explanation:Because we multiplied the values by 2 in the copy function.
Output:
x = 2,y = 6,z = 14

Question 38 ARICENT

    #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;

    }


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

Question 39 ABB

    #include <iostream>

    using namespace std;

    void square (int *x)

    {

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

    }

    int main ( )

    {

       int num = 10;

        square(&num);

        cout << num;

        return 0;

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

Question 40 HCL

    #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;

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

Question 41 INFOSYS

    #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;

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

Question 42 TECH MAHINDRA

    #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;

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

Question 43 TCS

    #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;

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

Question 44 WIPRO

    #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);

    }
Explanation: We are multiplying these values by adding every values.
Output:
25

Answer:a
Explanation:In function overloading, we can use any number of arguments but same function name.

Question 45 HCL

    #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);

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

Question 46 SATYAM

    #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;

    }
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:
5500.263

Question 47 HCL

    #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.

Question 48 HONEYWELL

    #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;

    }
Explanation:In this program, we are divide and multiply the values.
Output:

10 2.5


Question 49 WIPRO

    #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;

    }
Explanation:In this program, we are passing the value, as it evaluates to false, it produces the output as following.
Output:
Flag is false. a = 200

Question 50 TECH MAHINDRA

    #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:
Please enter a number:
5
Here is your number:5

Question 51 HCL


    #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;

    }
Explanation:In this program, We are passing the values as by default values rules it is working.
Output:
1st value: 1
2nd value: 10
1st value: 3
2nd value: 4

Question 52 SAMSUNG

    #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 infront of the default argument.

Question 53 SAMSUNG

    #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;

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

Question 54 HCL

    #include <iostream>

    using namespace std;

    void func(int x)

    {

        cout << x ;

    }

    int main()

    {

        void (*n)(int);

        n = &func;

        (*n)( 2 );

        n( 2 );

        return 0;

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

Question 55 SAMSUNG

    #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;

    }
Explanation:In this program, we have declared the values as integer instead of character, So it is printing as d99 but it will not arise an error.
Output:
d9
9

Question 56 WIPRO

    #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.

Question 57 TCS

    #include <iostream>

    using namespace std;

    #define MIN(a,b) (((a)<(b)) ? a : b)

    int main ()

    {

        float i, j;

        i = 100.1;

        j = 100.01;

        cout <<"The minimum is " << MIN(i, j) << endl;

        return 0;

    }
Explanation:In this program, we are getting the minimum number using conditional operator.
Output:
The minimum value is 100.01

Question 58 HCL

    #include <iostream>

    using namespace std;

    int main ()

    {

        cout << "Value of __LINE__ : " << __LINE__ << endl;

        cout << "Value of __FILE__ : " << __FILE__ << endl;

        cout << "Value of __DATE__ : " << __DATE__ << endl;

        cout << "Value of __TIME__ : " << __TIME__ << endl;

        return 0;

    }
Explanation:In this program, we are using the macros to print the information about the file.
Output:
Value of __LINE__ : 5
Value of __FILE__ : mac1.cpp
Value of __DATE__ : Oct 10 2012
Value of __TIME__ : 22:24:37

Question 59 TCS

    #include <iostream>

    using namespace std;

    #define SquareOf(x) x * x

    int main()

    {

        int x;

        cout << SquareOf(x + 4);

        return 0;

    }
Explanation:In this program, as we haven’t initiailzed the variable x, we will get a output of ending digit of 4.
Output:
75386824

Question 60 SAMSUNG

    #include <iostream>

    using namespace std;

    #define PR(id) cout << "The value of " #id " is "<<id

    int main()

    {

        int i = 10;

        PR(i);

        return 0;

    }
Explanation:In this program, we are just printing the declared values.
Output:
10

Question 61 WIPRO

    #include <iostream>

    using namespace std;

    #define MAX 10

    int main()

    {

        int num;

        num = ++MAX;

        cout << num;

        return 0;

    }

Answer: compile time error
Explanation:Macro Preprocessor only replaces occurance of macro symbol with macro symbol value, So we can’t increment the value.

Question 62 TCS

    #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;

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

Question 63 INFOSYS

    #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;

    }
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:
10

Question 64 HCL

    #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;

    }
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:
16

Question 65 INFOSYS

    #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;

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

Question 66 TCS

    #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:In this program, some lines of code are compiled, therefore thus the executed value is printed as 9.


Question 67 HCL

    #include <iostream>

    using namespace std;

    int main()

    {

        char name[30];

        cout << "Enter name: ";

        gets(name);

        cout << "Name: ";

        puts(name);

        return 0;

    }

Answer: compile time error
Explanation:In this program,we need to string header file to run this program.


Question 68 TCS

    #include <iostream>

    using namespace std;

    class rect

    {

        int x, y;

        public:

        void val (int, int);

        int area ()

        {

            return (x * y);

        }

    };

    void rect::val (int a, int b)

    {

        x = a;

        y = b;

    }

    int main ()

    {

        rect rect;

        rect.val (3, 4);

        cout << "rect area: " << rect.area();

        return 0;

    }
Explanation:In this program, we are calculating the area of rectangle based on given values.
Output:
rect area: 12

Question 69 INFOSYS

    #include <iostream>

    using namespace std;

    class CDummy

    {

        public:

        int isitme (CDummy& param);

    };

    int CDummy::isitme (CDummy& param)

    {

        if (&param == this)

            return true;

        else

            return false;

    }

    int main ()

    {

        CDummy a;

        CDummy *b = &a;

        if (b->isitme(a)) {

            cout << "execute";

        }

        else

        {

            cout<<"not execute";

        }

        return 0;

    }
Explanation:In this program, we are just pointing the pointer to a object and printing execute if it is correctly pointed.
Output:
execute

Question 70 WIPRO

    #include <iostream>

    using namespace std;

    int main()

    {

        typedef int num;

        num a = 10, b = 15;

        num c = a + b + a - b;

        cout << c;

        return 0;

    }
Explanation:In this program, we are manipulating the numbers and printing the result using user-defined data types.
Output:
20

Question 71 SAMSUNG

    #include <iostream>

    using namespace std;

    int main()

    {

        int i;

        enum month {

            JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC

        };

        for (i = JAN; i <= DEC; i++)

            cout << i;

        return 0;

    }
Explanation:In this program, we are defined the data types as enumerator and printing its value in a order.
Output:
012345678910

Question 72 HCL

    #include <iostream>

    using namespace std;

    int main()

    {

        typedef int num;

        typedef char let;

        let w = "steve";

        num a = 10, b = 15;

        num c = a + w;

        cout << c;

        return 0;

    }

Answer: compile time error
Explanation:In this program, we are trying to add an integer and a character and it is not possible.

Question 73 TCS

    #include <iostream>

    using namespace std;

    class Box

    {

        public :

        double length;

        double breadth;

        double height;

    };

    int main( )

    {

        Box Box1;

        double volume;

        Box1.height = 5;

        Box1.length = 6;

        Box1.breadth = 7.1;

        volume = Box1.height * Box1.length * Box1.breadth;

        cout << "Volume of Box1 : " << volume <<endl;

        return 0;

    }
Explanation:In the above program, we are calculating the area of the cube by using the cube formula
Output:
213

Question 74 SAMSUNG

    #include <iostream>

    using namespace std;

    class Rect

    {

        int x, y;

        public:

        void set_values (int,int);

        int area ()

        {

            return (x * y);

        }

    };

    void Rect::set_values (int a, int b) {

        x = a;

        y = b;

    }

    int main ()

    {

        Rect recta, rectb;

        recta.set_values (5, 6);

        rectb.set_values (7, 6);

        cout << "recta area: " << recta.area();

        cout << "rectb area: " << rectb.area();

        return 0;

    }

Answer: recta area: 30 rectb area: 42
Explanation:We are calculating the area of rectangle by two objects.

Answer:d
Explanation:Because a class may contain any number of objects according to it’s compliance.

Question 75 ABB

    #include <iostream>

    using namespace std;

    class sample

    {

        private:

        int var;

        public:

        void input()

        {

           cout << var;

        }

        void output()

        {

           cout << "Variable entered is ";

           cout << var << "\n";

        }

    };

    int main()

    {

        sample object;

        object.input();

        object.output();

        object.var();

        return 0;

    }

Answer: error
Explanation:While using private member, you can’t access it variable.

Answer: a
Explanation:None.

Question 76 HCL

    #include <iostream>

    using namespace std;

    class number

    {

        int i;

        public:

        int geti();

        void puti(int j);

    };

    int number::geti()

    {

        return i;

    }

    void number::puti(int j)

    {

        i = j;

    }

    int main()

    {

        number s;

        s.puti(10);

        cout << s.geti( );

        return 0;

    }
Explanation:We are getting the number and copying it to j and printing it.
Output:
10

Question 77 TCS

    #include <iostream>

    using namespace std;

    class sample

    {

        public:

        int x, y;

        sample() {};

        sample(int, int);

        sample operator + (sample);

    };

    sample::sample (int a, int b)

    {

        x = a;

        y = b;

    }

    sample sample::operator+ (sample param)

    {

        sample temp;

        temp.x = x + param.x;

        temp.y = y + param.y;

        return (temp);

    }

    int main ()

    {

        sample a (4,1);

        sample b (3,2);

        sample c;

        c = a + b;

        cout << c.x << "," << c.y;

        return 0;

    }
Explanation:In this program, we are adding the first number of a with first number of b by using opertor function and also we are adding second number by this method also.
Output:
7, 3

Question 78 HCL

    #include <iostream>

    using namespace std;

    class Box

    {  

        double length;

        double breadth;

        double height;

        public:

        double getVolume(void)

        { 

            return length * breadth * height;

        }

        void setLength( double len )

        {  

            length = len;

        }

        void setBreadth( double bre )

        {  

            breadth = bre;

        }

        void setHeight( double hei )

        {  

            height = hei;

        }

        Box operator+(const Box& b)

        { 

            Box box;

            box.length = this->length + b.length;

            box.breadth = this->breadth + b.breadth;

            box.height = this->height + b.height;

            return box;

        }

    };

    int main( )

    { 

        Box Box1;

        Box Box2;

        Box Box3;

        double volume = 0.0;

        Box1.setLength(6.0);

        Box1.setBreadth(7.0);

        Box1.setHeight(5.0);

        Box2.setLength(12.0);

        Box2.setBreadth(13.0);

        Box2.setHeight(10.0);

        volume = Box1.getVolume();

        cout << "Volume of Box1 : " << volume <<endl;

        volume = Box2.getVolume();

        cout << "Volume of Box2 : " << volume <<endl;

        Box3 = Box1 + Box2;

        volume = Box3.getVolume();

        cout << "Volume of Box3 : " << volume <<endl;

        return 0;

    }
Explanation:In this program, we finding the box3 area by adding box1 and box2.
Output:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400


Question 79 SAMSUNG

    #include <iostream>

    using namespace std;

    class sample1

    {

        float i, j;

    };

    class sample2

    {

        int x, y;

        public:

        sample2 (int a, int b)

        {

             x = a;

             y = b;

        }

        int result()

        {

             return x + y;

         }

    };

    int main ()

    {

        sample1 d;

        sample2 * padd;

        padd = (sample2*) &d;

        cout < result();

        return 0;

    }
Explanation:As it assigns to a reference to an object of another incompatible type using explicit type-casting.
Output:
14032334

Question 80 ORACLE

    #include

    using namespace std;

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

    {

        a *= 2;

        b *= 2;

        c *= 2;

    }

    int main ()

    {

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

        duplicate (x, y, z);

        cout << x << y << z;

        return 0;

    }
Explanation:We are passing the values by reference and modified the data on the function block.
Output:
2614

Question 81 TCS

    #include

    using namespace std;

    int operate (int a, int b)

    {

        return