31 Jan 2014

Basics c++ practice mathematical operator set 1

MATHEMATICAL OPERATOR
These are basic level questions asked in various companies. This is practice paper for C++ Software professionals. You are most welcome to give feedback for these questions in comments sections. 

Question 1 INFOSYS
   #include <iostream>
    using namespace std;
    int main ()
    {
        cout << (3 > 4 && 3 > 1) << endl;
        return 0;
    }
a) 0
b) 1
c) error
d) it will compile but not run
View Answer
Answer:a
Explanation:The logical and operator will return true when both are evaluates to true.
Output:
$ g++ bool.cpp
$ a.out
0
Question 2 TCS
#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;
    }
a) 55
b) 62
c) 52
d) none of the mentioned
View Answer
Answer:c
Explanation:((5 * 10) +(1 + 1));
Output:
$ g++ bool1.cpp
$ a.out
52

Question 3 HCL
#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
Explanation:a and b are converted into integers and are assigned the values 0 and 1 respectively.Similarly we are taking bitwise or between x and y. And then we finally add both the values and got the output as 16.
Question 4 TECH MAHINDRA
#include <iostream>
    using namespace std;
    int main()
    {
        for (int i = 64; i < 69; i++)
            cout << (char) i;
        return 0;
    }
Answer: @ABCD
Explanation:The ASCII values from 64 to 69 will be printed using this program. So it will print as @ABCD.
Question 5 HONEYWELL
#include <iostream>
    using namespace std;
    int main()
    {
        cout << '112';
        return 0;
    }
Answer: J
Explanation:The literal value for 112 is J. So it will be printing J.
Question 6 TCS
#include <iostream>
   using namespace std;
   int main()
   {
       char ch = 'c'
       cout << (int(c) + 1);
       return 0;
   }
Answer: 100
Explanation:The ASCII value of c is 99. hence after the expression is evaluated the result 100 is printed.
Question 7 SAPIENT
#include <iostream>
    using namespace std;
    int main()
    {
        int a = 8;
        cout << "ANDing integer 'a' with 'true' :" << a && true;
        return 0;
    }
Explantion:If we did the ‘and’ operation between a and true means, there ascii value will be compared and the output is retruned as 8.
Output:
ANDing integer ‘a’ with ‘true’ :8
Question 8 HCL
#include <iostream>
    using namespace std;
    int i;
    void increment( int i )
    {
        i++;
    }
    int main()
    {
        for (i = 0; i < 10; increment( i ))
        {
            cout << i;
        }
        return 0;
    }
Explanation:While executing the for loop the function will be called at the end and while returning the value the main function should execute from the beginning only, so the loop occurs.
Output:
0000000000000000000000000….
Question 9 SAMSUNG
#include <iostream>
    using namespace std;
    int main()
    {
        int i = 3;
        int l = i / -2;
        int k = i % -2;
        cout << l << k;
        return 0;
    }
Explanation:sign of result of mod oepration on negative numbers is sign of dividend.
Output:
-1 1
Question 10 WIPRO
#include <iostream>
    using namespace std;
    int main()
    {
        int i = -3;
        int k = i % 2;
        cout << k;
        return 0;
    }
Explanation:sign of result of mod operation on negative numbers is sign of dividend.
Output:
-1
Question 11 TCS
#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;
    }
Explanation:Float store floating point numbers with 8 place accuracy and requires 4 bytes of Memory. Double has 16 place accuracy having size of 8 bytes.
Output:
Harvard
Question 12 HCL
#include <iomanip>
    #include <iostream>
    using namespace std;
    int main()
    {
        cout << setprecision(17);
        double d = 0.1;
        cout << d << endl;
        return 0;
    }
Explantion:The double had to truncate the approximation due to it’s limited memory, which resulted in a number that is not exactly 0.1.
Output:
0.10000000000000001
Question 13 MIND TREE
#include <iostream>
    using namespace std;
    int main()
    {
        float i = 123.0f;
        cout << i << endl;
        return 0;
    }

Explanation:The value 123 is printed because of its precision.
Output
123
Question 14 SAPIENT
#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;
    }
Explanation:0.5f results in 0.5 to be stored in floating point representations.
Output:
equal
Question 15 ABB
#include <iostream>
    using namespace std;
    int main()
    {
        int num = 0x20 + 020 + 20;
        cout << sizeof(num)<<'n';
        return 0;
    }
Explanation:The sum of three numbers are belongs to different number systems, so the result is typecasted into integer.
Output:
4
Question 16 HCL
#include <iostream>
    using namespace std;
    int main ( )
    {
        static double i;
        i = 20;
        cout << sizeof(i);
        return 0;
    }

Explanation:The size of the double data type is 8.
Ouput
8
Question 17 TCS
#include <iostream>
    using namespace std;
    int main()
    {
        int num1 = 10;
        float num2 = 20;
        cout << sizeof(num1 + num2);
        return 0;
    }
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:
4
Question 18 INFOSYS
#include <iostream>
    using namespace std;
    int main()
    {
        int a = 5;
        float b;
        cout << sizeof(++a + b);
        cout << a;
        return 0;
    }
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:
4 5
Question 19 SAMSUNG
#include <iostream>
    using namespace std;
    int main()
    {
        char y = 'a';
        int x = 10;
        float z = 10.1;
        cout << sizeof(char);
        cout << sizeof(int);
        cout << sizeof(float);
        return 0;
    }
Answer: char < int < float
Explanation:char will have the least size in this program and int will have next least size and float will have the highest size.
Question 20 HONEYWELL
#include <iostream>
    using namespace std;
    int main()
    {
        void a = 10, b = 10;
        int c;
        c = a + b;
        cout << c;
        return 0;
    }
Answer: compile time error
Explanation:void will not accept any values to its type.
Question 21 HCL
#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;
    }
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.
Output
If you were cat, you would be 2
Question 22 WIPRO
#include <iostream>
    using namespace std;
    enum test {
        A = 32, B, C
    };
    int main()
    {
        cout << A << B<< C;
        return 0;
    }
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:
323334
Question 23 HONEYWELL
    #include <iostream>
    using namespace std;
    int main()
    {
        int a;
        a = 5 + 3 * 5;
        cout << a;
        return 0;
    }
Explanation:Because the * operator is having highest precedence, So it is executed first and then the + operator will be executed.
Output:
20
Question 24  BIRLA SOFT
    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 5, b = 6, c, d;
        c = a, b;
        d = (a, b);
        cout << c << 't' << d;
        return 0;
    }
Explanation:It is a separtor 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:
5 6
Question 25 HCL
    #include <iostream>
    using namespace std;
    int main()
    {
        int i, j;
        j = 10;
        i = (j++, j + 100, 999 + j);
        cout << i;
        return 0;
    }
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:
1010
Question 26 TCS
    #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;
    }
Explanation:Because of the precedence the pre-increment and post increment operator, we got the output as 749736.
Output:
749736
Question 27 WIPRO
    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 5, b = 6, c;
        c = (a > b) ? a : b;
        cout << c;
        return 0;
    }
Explanation:Here the condition is false on conditional operator, so the b value is assigned to c.
Output:
6
Question 28 SAMSUNG
    #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 <<'t'<< d;
        return 0;
    }
Explanation:In this program, we are casting the operator to integer, So it is printing as 21 and 10
Output:
21 10
Question 29 TCS
    #include <iostream>
    using namespace std;
    int main ()
    {
        int n;
        for (n = 5; n > 0; n--)
        {
            cout << n;
            if (n == 3)
                break;
        }
        return 0;
    }
Explanation:Inthis program, We are printing the numbers in reverse order but by using break statement we stopped printing on 3.
Output:
543
Question 30 HCL
    #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.
Question 31 SAMSUNG
    #include <iostream>
    using namespace std;
    int main()
    {
        int n = 15;
        for ( ; ;)
        cout << n;
        return 0;
    }
Answer:  infinite times of printing n
Explanation:There is not a condition in the for loop, So it will loop continuously.
Question 32 TCS
    #include <iostream>
    using namespace std;
    int main()
    {
        int i;
        for (i = 0; i < 10; i++);
        {
            cout << i;
        }
        return 0;
    }
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:
10
Question 33 HCL
    #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.
Question 34 TECH MAHINDRA
    #include <iostream>
    using namespace std;
    int main ()
    {
        int a, b;        
        a = 10;          
        b = 4;           
        a = b;          
        b = 7;          
        cout << "a:";
        cout << a;
        cout << " b:";
        cout << b;
        return 0;
    }
Explanation:In this program, we are reassigning the values of a and b because of this we got the output as a:4 b:7
Output:
a:4 b:7
Question 35 SAMSUNG
    #include <iostream>
    using namespace std;
    int main ()
    {
        int a, b, c;
        a = 2;
        b = 7;
        c = (a > b) ? a : b;
        cout << c;
        return 0;
    }
Explanation:We are using the ternary operator to evaluate this expression. It will return first option, if first condition is true otherwise it will return second
Output:
7
Question 36 HCL
    #include <iostream>
    using namespace std;
    int main()
    {
        int a = 0;
        int b = 10;
        if ( a && b )
        {
            cout << "true"<< endl ;
        }
        else
        {
            cout << "false"<< endl ;
        }
        return 0;
    }
Explanation:&& is called as Logical AND operator, if there is no zero in the operand means, it will be true otherwise false.
Output:
false
Question 37 SAMSUNG
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int a = 21;
        int c ;
        c = a++;
        cout << c; 
        return 0;
    }
Explanation:value of ‘a’ will be stored in c and then only it will be incremented.
Output:
21
Question 38 INFOSYS
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int x = 5, y = 5;
        cout << ++x << --y << endl;
        return 0;
    }
Explanation:The values will be preincemented and predecremented, So it will print as 64.
Output:
64
Question 39 HCL
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int x = 5, y = 5, z;
        x = ++x; y = --y;
        z = x++ + y--;
        cout << z;
        return 0;
    }
Explanation:In this program, the increment and decrement of evaluation of z will not be accounted because they are post.
Output:
10
Question 40 TCS
    #include <stdio.h> 
    using namespace std;
    int main()
    {
        int x = 5, y = 5, z;
        x = ++x; y = --y;
        z = x + ++x;
        cout << z;
        return 0;
    }
Explanation:In this program, we are adding the x value after preincrementing two times.
Output:
14
Question 41 SAMSUNG
    #include <stdio.h>
    using namespace std;
    int main()
    {
        int num1 = 5;
        int num2 = 3;
        int num3 = 2;
        num1 = num2++;
        num2 = --num3;
        cout << num1 << num2 << num3;
        return 0;
    }
Explanation:In this program, We are preincrementing and postincrementing the operands and saving it.
Output:
311






1 comment: