TCS Ninja Programming logic Quiz

1. What is the output of the following program?

#include<iostream>

using namespace std;

class PM

{

   public: 

   void designation()

    {

       cout << “PM”;

    }

};

class CEO

{

        public:

         CEO()

          {

            pmPtr = new PM;

          }

         PM* operator -> () 

          {

             return pmPtr;

          }

         void designation()

          {

             cout << “CEO”;

          }

          private: PM* pmPtr;
};

int main() 

{

     CEO* ceoPtr;

     ceoPtr = new CEO;

     ceoPtr ->designation();

     delete ceoPtr;

}

PM

PM

20.51%

CEO

CEO

48.72%

Compilation Error

Compilation Error

23.08%

Runtime Error

Runtime Error

7.69%

Answer : CEO

Explanation : 

Here as we all known that execution starts from the main function so in the main function we have created object of the class CEO and called the designation method which  eventually contains print("CEO") So the outut will be "CEO".

2. The Output when the following code is executed ?

#include<stdio.h>

int main()

{

     int x=2; 

     if(x--, --x, x)

          printf(“TCS TNQT exam”);

      else 

          printf(“TCS Ninja exam”);

     return 0;

}

Compilation error - invalid if statement

Compilation error - invalid if statement

26.47%

TCS TNQT exam

TCS TNQT exam

26.47%

TCS Ninja exam

TCS Ninja exam

44.12%

Runtime Error

Runtime Error

2.94%

Answer : TCS Ninja exam

Explanation :

In this program the if statement results in false So, it will print the else part i.e TCS Ninja exam

3. What is the equivalent while loop for the given  for loop given below ?

for(int i=0,j=5, k=10; i<10; i++,j+=5)

 {

      printf(“%d”,j);

  }

int i=0 int j=5,k=10;

while(i<10)

{     

     printf(“%d”,j);     

     j=j+5;   

     i=i+1;

}

int i=0 int j=5,k=10;

while(i<10)

{     

     printf(“%d”,j);     

     j=j+5;   

     i=i+1;

}

55.17%

int i=0; int j=5,k=10;

while(i<10)

{  

      j=j+5;  

      i=i+1;  

      printf(“%d”,j);

}

int i=0; int j=5,k=10;

while(i<10)

{  

      j=j+5;  

      i=i+1;  

      printf(“%d”,j);

}

17.24%

int i=0; int j=5,k=10;

while(j<k)

   

        j=j+5;  

        i=i+1;

}

int i=0; int j=5,k=10;

while(j<k)

   

        j=j+5;  

        i=i+1;

}

10.34%

int i=0;

int j=5,k=10;

while(i<k)

{  

       j=j+5;  

       i=i+1;   

       printf("%d",j);

}

int i=0;

int j=5,k=10;

while(i<k)

{  

       j=j+5;  

       i=i+1;   

       printf("%d",j);

}

17.24%

Answer :

int i=0 int j=5,k=10;

while(i<10)

{     

     printf(“%d”,j);     

     j=j+5;   

     i=i+1;

}
Explanation :

4. In C, there are functions like putchar (int C), fputc(int FILE*), write (int, const void*, size_t)........In an object oriented programming language like C++, all these functions can have same name like write (int c), write (int, const void*, size_t). This feature is called ?

Function Prototyping

Function Prototyping

24.14%

Function Modularizing

Function Modularizing

20.69%

Function Overloading

Function Overloading

44.83%

Function Grouping

Function Grouping

10.34%

Answer : Function Overloading

Explanation :

As we know that Function Overloading is the concept in which we have the same function same but can have different function signature.

5.

The following program is supposed to find the number of terms in the  num array. What should be written in the missing last__ before the while loop’s closing brace? Write your answer without any leading, trailing, Interspersed blank spaces or semicolons ?

int num_array[]={17,20,23,26, 29, 32, 35};

int count = 0; 

while (count < sizeof(num_array/ num_array[0]))

{

      //Replace this commented line with the required C statement. No semicolon.

 } 

    printf(“%d”, num_array[count]);

    

}

printf(“There are %d elements in the array \n”count);

count = count + 2

count = count + 2

16.67%

count++

count++

70.83%

count--

count--

4.17%

count* = 1

count* = 1

8.33%

Answer : count++

Explanation :

Here we have initialized count variable = 0, and in while loop condition remains true till the value of count becomes equal to the number of elements.

Iteration 1 : while(0 < 7)

               {

                  count++;

               }
Iteration 2 : while(1 < 7)

             {

                count++;

             }
Iteration 3 : while(3 < 7)

             {

                count++;

             }
Iteration 4 : while(4 < 7)

             {

                count++;

             }
Iteration 5 : while(5 < 7)

             {

                count++;

             }
Iteration 6 : while(6 < 7)

             {

                count++;

             }
Iteration 7 : while(7 < 7) // false

             {

                count++;

                     
             }

6.  What is the output of the following JAVA program ?

import java.lang.*  

public class MyThread extends Thread

{

     public void run()

      {

          System.out.println(“Before”);

          this.stop();

          System.out.printIn(“After”);

       }

public static void main (String[]args)

 {

      MyThread a = new MyThread();

      a.start();

   }

}

Before

Before

43.48%

After

After

17.39%

Before

After

Before

After

30.43%

None of These

None of These

8.7%

Answer : Before

Explanation : 

This program is related to thread Concepts and in the main function we are creating new thread by using the Thread class and we are calling  a.start( ) method so it will print "Before" and after that it calls the stop( ) method so it kill the thread and it will not print anything after that.

7. What is the value of result in the following C program ?

int a = -10, b = -5, flag = 1, result = 0, i;

int x = abs(a); 

int y = abs(b);

for(i=1; i<=x; i++)

     result + = y;

if((a>=0 && b<0) || (a<0 && b>=0))

     flag = -1

result *= flag;

40

40

10.71%

50

50

71.43%

30

30

10.71%

20

20

7.14%

Answer : 50

Explanation : 

x = 10  // As we are taking the absolute value of x

y = 5   // // As we are taking the absolute value of y

Iteration 1:  i =1 ----    i<=10(true) --- i++

result = 0+5 = 5

Iteration 2:  i =2 ----    i<=10(true) --- i++

result = 5+5 = 10

Iteration 3:  i =3 ----    i<=10(true) --- i++

result = 10+5 = 15

Iteration 4:  i =4 ----    i<=10(true) --- i++

result = 15+5 = 20

Iteration 5:  i =5 ----    i<=10(true) --- i++

result = 20+5 = 25

Iteration 6:  i =6----    i<=10(true) --- i++

result = 25+5 = 30

.

.

.

Iteration 10:  i =1 ----    i<=10(true) --- i++

result = 45+5 = 50

Iteration 11:  i =1 ----    i<=10(false) --- i++

Loop breaks....

 

8.  In a Class diagram, there is an arrow that starts at A and points at B. It means ?

A is parent class of B

A is parent class of B

50%

B is parent class of A

B is parent class of A

38.46%

A and B are loosely coupled

A and B are loosely coupled

3.85%

A and B are tightly coupled

A and B are tightly coupled

7.69%

Answer : B is a Parent Class of A

Explanation : 

(child)A------------------->B(Parent)

In the class diagram  the arrow always points to parent.

9. What is the value of maximum, if the following segment runs to completion ?

#include<limits.h>

int main()

{

       int i=0, maximum = INT_MAX;

       int numbers[] = {923, 948, 988, 981, 167, 899, 200, 910, 999};

       while(i<sizeof(numbers)/sizeof(numbers[0]))

          {

                  if (maximum<numbers[i])

                    {

                            maximum=numbers[i]; 

                    }

                    i++;

         }

}

2147483647

2147483647

45.83%

21475683647

21475683647

25%

999

999

29.17%

789

789

0%

Answer : 2147483647

Explanation :

In this Snippet the maximum variable initialized with INT_MAX =2147483647(The value of INT_MAX = 2147483647) and the if condition never becomes true because so at last the maximum variable contains the value = 2147483647.

10. What is the output of the following Java program ?

Class Super

{

    Static String greeting() 

     {

        return “Goodnight”;

     }

     String name()

      {

         return “Ram”;

      }

}

Class Sub extends Super

{

    Static String greeting ()

      {

          return “Hello”;

      }

     String name() 

      {

          return “Bheem”;

      }

}

Public class Test

{

      Public static void main {String[]args}

        {

               Super s=new Sub();

               System.outprintln(s.greeting()+”,”+s.name());

        }

}

Good night Ram

Good night Ram

34.48%

Good night Bheem

Good night Bheem

48.28%

Hello Bheem

Hello Bheem

13.79%

Hello Ram

Hello Ram

3.45%

Answer : Good night Bheem

Explanation : 

As we Know the execution starts from the main( ) function so in the main function we have created object of Super class and using that object we have called the function s.greeting( ) will be called for Super class because when inheriting from another class,Super() has to be called first in the Constructor .This is why the super Class method is invoked first and then the Sub class method.

×

Please login to report