Sunday, 23 August 2015

geek1--Loops & Control Structure--B

Question 12
CORRECT
#include <stdio.h>  
int main()
{
  int i;  
  for (i = 1; i != 10; i += 2)
    printf(" GeeksQuiz ");
  return 0;
}
A
GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz
GeeksQuiz GeeksQuiz GeeksQuiz .... infinite times
C
GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz
D
GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz GeeksQuiz

Discuss it


Question 12 Explanation: 
The loop termination condition never becomes true and the loop prints GeeksQuiz infinite times. In general, if a for or while statement uses a loop counter, then it is safer to use a relational operator (such as <) to terminate the loop than using an inequality operator (operator !=).
Question 13
CORRECT
What will be the output of the following C program segment? (GATE CS 2012)
char inchar = 'A';
switch (inchar)
{
case 'A' :
    printf ("choice A \n") ;
case 'B' :
    printf ("choice B ") ;
case 'C' :
case 'D' :
case 'E' :
default:
    printf ("No Choice") ;
}
A
No choice
B
Choice A
Choice A
Choice B No choice
D
Program gives no output as it is erroneous

Discuss it


Question 13 Explanation: 
There is no break statement in case ‘A’. If a case is executed and it doesn’t contain break, then all the subsequent cases are executed until a break statement is found. That is why everything inside the switch is printed. Try following program as an exercise.
int main()
{
    char inchar = 'A';
    switch (inchar)
    {
    case 'A' :
        printf ("choice A \n") ;
    case 'B' :
    {
        printf ("choice B") ;
        break;
    }
    case 'C' :
    case 'D' :
    case 'E' :
    default:
        printf ("No Choice") ;
    }
}
Question 14
CORRECT
Predict the output of the below program:
#include <stdio.h>
int main()
{
    int i = 3;
    switch(i)
    {
        printf("Outside ");
        case 1: printf("Geeks");
            break;
        case 2: printf("Quiz");
            break;
        defau1t: printf("GeeksQuiz");
    }
    return 0;
}
A
Outside GeeksQuiz
B
GeeksQuiz
Nothing gets printed

Discuss it


Question 14 Explanation: 
In a switch block, the control directly flows within the case labels(or dafault label). So, statements which do not fall within these labels, Outside is not printed. Please take a closer look at the default label. Its defau1t, not default which s interpreted by the compiler as a label used for goto statements. Hence, nothing is printed in the above program.
Question 15
CORRECT
In the following program, X represents the Data Type of the variable check.
#include <stdio.h>
int main()
{
    X check;
    switch (check)
    {
        // Some case labels
    }
    return 0;
}
Which of the following cannot represent X?
A
int
B
char
C
enum
float

Discuss it


Question 15 Explanation: 
A switch expression can be int, char and enum. A float variable/expression cannot be used inside switch.
Question 16
****CORRECT
What is the output of the following program?
#include <stdio.h>
int main()
{
    char check = 'a';
    switch (check)
    {
        case 'a' || 1: printf("Geeks ");
         
        case 'b' || 2: printf("Quiz ");
                    break;
        default: printf("GeeksQuiz");
    }
    return 0;
}
A
Geeks
B
Geeks Quiz
C
Geeks Quiz GeeksQuiz
Compile-time error

Discuss it


Question 16 Explanation: 
An expression gets evaluated in a case label. Both the cases used are evaluated to 1(true). So compile-time error: duplicate case value is flashed as duplicated cases are not allowed.
Question 17
***********WRONG
Predict the output of the following program:
#include <stdio.h>
int main()
{
    int check = 20, arr[] = {10, 20, 30};
    switch (check)
    {
        case arr[0]: printf("Geeks ");
        case arr[1]: printf("Quiz ");
        case arr[2]: printf("GeeksQuiz");
    }
    return 0;
}
A
Quiz
Quiz GeeksQuiz
C
GeeksQuiz
Compile-time error

Discuss it


Question 17 Explanation: 
The case labels must be constant inside switch block. Thats why the compile-time error: case label does not reduce to an integer constant is flashed.
Question 18
CORRECT
How many times GeeksQuiz is printed
#include<stdio.h>
int main()
{
    int i = -5;
    while (i <= 5)
    {
        if (i >= 0)
            break;
        else
        {
            i++;
            continue;
        }
        printf("GeeksQuiz");
    }
    return 0;
}
A
10 times
B
5 times
C
Infinite times
0 times

Discuss it


Question 18 Explanation: 
The loop keeps incrementing i while it is smaller than 0. When i becomes 0, the loop breaks. So GeeksQuiz is not printed at all.
Question 19
CORRECT
#include <stdio.h>
int main()
{
    int i = 3;
    while (i--)
    {
        int i = 100;
        i--;
        printf("%d ", i);
    }
    return 0;
}
A
Infinite Loop
99 99 99
C
99 98 97
D
2 2 2

Discuss it


Question 19 Explanation: 
Note that the i–- in the statement while(i-–) changes the i of main() And i== just after declaration statement int i=100; changes local i of while loop.
Question 20
****WRONG
#include <stdio.h>
int main()
{
    int x = 3;
    if (x == 2); x = 0;
    if (x == 3) x++;
    else x += 2;
 
    printf("x = %d", x);
 
    return 0;
}
x = 4
x = 2
C
Compiler Error
D
x = 0

Discuss it


Question 20 Explanation: 
Value of x would be 2. Note the semicolon after first if statement. x becomes 0 after the first if statement. So control goes to else part of second if else statement.
Question 21
***WRONG
#include<stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
    default:
        a = 4;
    case 6:
        a--;
    case 5:
        a = a+1;
    case 1:
        a = a-1;
    }
    printf("%d \n", a);
    return 0;
}
3
B
4
5
D
None of these

Discuss it

note-- execution from default on ward starts only whwn there is no matching 
Question 21 Explanation: 
There is no break statement, so first a = a + 1 is executed, then a = a-1 is executed.
Question 22
CORRECT
Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression?
a = ( x > y ) ? (( x > z ) ? x : z) : (( y > z ) ? y : z )
x = 3, y = 4, z = 2
B
x = 6, y = 5, z = 3
C
x = 6, y = 3, z = 5
D
x = 5, y = 4, z = 5

Discuss it


Question 22 Explanation: 
The given expression assigns maximum among three elements (x, y and z) to a.
Question 23
WRONG
Consider the C program below.
#include <stdio.h>
int *A, stkTop;
int stkFunc (int opcode, int val)
{
    static int size=0, stkTop=0;
    switch (opcode)
    {
    case -1:
        size = val;
        break;
    case 0:
        if (stkTop < size ) A[stkTop++]=val;
        break;
    default:
        if (stkTop) return A[--stkTop];
    }
    return -1;
}
int main()
{
    int B[20];
    A=B;
    stkTop = -1;
    stkFunc (-1, 10);
    stkFunc (0, 5);
    stkFunc (0, 10);
    printf ("%d\n", stkFunc(1, 0)+ stkFunc(1, 0));
}
The value printed by the above program is ___________
A
9
10
15
D
17

Discuss it


Question 23 Explanation: 
The code in main, basically initializes a stack of size 10, then pushes 5, then pushes 10. Finally the printf statement prints sum of two pop operations which is 10 + 5 = 15.
    stkFunc (-1, 10);   // Initialize size as 10
    stkFunc (0, 5);    // push 5
    stkFunc (0, 10);   // push 10

    // print sum of two pop
    printf ("%d\n", stkFunc(1, 0) + stkFunc(1, 0));
You have completed 23/23 questions .
Your score is 61%.

No comments:

Post a Comment