C Programming

C Programming

Control Instructions has 52 Questions
How many times "ASTIWZ" is get printed?
#include<stdio.h>
int main()
{
    int x;
    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;
        else
            break;
        printf("ASTIWZ");
    }
    return 0;
}

Infinite times
11 times
0 times
10 times
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=0;
    for(; i<=5; i++);
        printf("%d,", i);
    return 0;
}

0, 1, 2, 3, 4, 5
5
1, 2, 3, 4
6
Point out the error, if any in the for loop.
#include<stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d", i++);
        if(i>10)
           break;
    }
    return 0;
}

There should be a condition in the for loop
The two semicolons should be dropped
The for loop should be replaced with while loop.
No error
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 10, j = 20;
    if(i = 5) && if(j = 10)
        printf("Have a nice day");
    return 0;
}

Output: Have a nice day
No output
Error: Expression syntax
Error: Undeclared identifier if
A short integer is at least 16 bits wide and a long integer is at least 32 bits wide.

True
False
The way the break is used to take control out of switch and continue to take control of the beginning of the switch?

Yes
No
How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
    int j=1;
    while(j <= 255)
    {
        printf("%c %d", j, j);
        j++;
    }
    return 0;
}

Infinite times
255 times
256 times
254 times
What will be the output of the program?
#include<stdio.h>
int main()
{
    char str[]="C-program";
    int a = 5;
    printf(a >10?"Ps":"%s", str);
    return 0;
}

C-program
Ps
Error
None of above
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int a = 10;
    switch(a)
    {
    }
    printf("This is c program.");
	return 0;
}

Error: No case statement specified
Error: No default specified
No Error
Error: infinite loop occurs
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 10, j = 15;
    if(i % 2 = j % 3)
        printf("ASTIWZ");
    return 0;
}

Error: Expression syntax
Error: Lvalue required
Error: Rvalue required
The Code runs successfully
If scanf() is used to store a value in a char variable then along with the value a carriage return( ) also gets stored it.

True
False
Can we use a switch statement to switch on strings?

Yes
No
Which of the following is not logical operator?

&
&&
||
!
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a = 500, b = 100, c;
    if(!a >= 400)
        b = 300;
    c = 200;
    printf("b = %d c = %d", b, c);
    return 0;
}

b = 300 c = 200
b = 100 c = garbage
b = 300 c = garbage
b = 100 c = 200
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("This is c program.");
        case 1:
            printf("Case1");
            break;
        case 2:
            printf("Case2");
            break;
    }
return 0;
}

Error: No default specified
Error: Invalid printf statement after switch statement
No Error and prints "Case1"
None of above
Point out the correct statements are correct about the program below?
#include<stdio.h>
int main()
{
    char ch;
    while(x=0;x<=255;x++)
        printf("ASCII value of %d character %c", x, x);
    return 0;
}

The code generates an infinite loop
The code prints all ASCII values and its characters
Error: x undeclared identifier
Error: while statement missing
The modulus operator cannot be used with a long double.

True
False
We want to test whether a value lies in the range 2 to 4 or 5 to 7. Can we do this using a switch?

Yes
No
In mathematics and computer programming, which is the correct order of mathematical operators ?

Addition, Subtraction, Multiplication, Division
Division, Multiplication, Addition, Subtraction
Multiplication, Addition, Division, Subtraction
Addition, Division, Modulus, Subtraction
What will be the output of the program?
#include<stdio.h>
int main()
{
    unsigned int i = 65535; /* Assume 2 byte integer*/
    while(i++ != 0)
        printf("%d",++i);
    printf("");
    return 0;
}

Infinite loop
0 1 2 ... 65535
0 1 2 ... 32767 - 32766 -32765 -1 0
No output
Point out the error, if any in the while loop.
#include<stdio.h>
int main()
{
    int i=1;
    while()
    {
        printf("%d", i++);
        if(i>10)
           break;
    }
    return 0;
}

There should be a condition in the while loop
There should be at least a semicolon in the while
The while loop should be replaced with for loop.
No error
Which of the following statements are correct about the program?
#include<stdio.h>
int main()
{
    int x = 30, y = 40;
    if(x == y)
        printf("x is equal to y");

    else if(x > y)
        printf("x is greater than y");

    else if(x < y)
        printf("x is less than y")
    return 0;
}

Error: Statement missing
Error: Expression syntax
Error: Lvalue required
Error: Rvalue required
A char variable can store either an ASCII character or a Unicode character.

True
False
By default, the data type of a constant without a decimal point is int, whereas the one with a decimal point is a double.

Yes
No
Which of the following cannot be checked in a switch-case statement?

Character
Integer
Float
enum
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x = 3;
    float y = 3.0;
    if(x == y)
        printf("x and y are equal");
    else
        printf("x and y are not equal");
    return 0;
}

x and y are equal
x and y are not equal
Unpredictable
No output
Which of the following errors would be reported by the compiler on compiling the program given below?
#include<stdio.h>
int main()
{
    int a = 5;
    switch(a)
    {
	case 1:
	printf("First");

	case 2:
	printf("Second");

	case 3 + 2:
	printf("Third");

	case 5:
	printf("Final");
	break;

    }
    return 0;
}

There is no break statement in each case.
Expression as in case 3 + 2 is not allowed.
Duplicate case case 5:
No error will be reported.
Which of the following statements are correct about an if-else statements in a C-program?
1: Every if-else statement can be replaced by an equivalent statements using   ? ; operators
2: Nested if-else statements are allowed.
3: Multiple statements in an if block are allowed.
4: Multiple statements in an else block are allowed.

1 and 2
2 and 3
1, 2 and 4
2, 3, 4
What will be the output of the program, if a short int is 2 bytes wide?
#include<stdio.h>
int main()
{
    short int i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
        printf("%u,", i);
    return 0;
}

1 ... 65535
Expression syntax error
No output
0, 1, 2, 3, 4, 5
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int P = 10;
    switch(P)
    {
       case 10:
       printf("Case 1");

       case 20:
       printf("Case 2");
       break;

       case P:
       printf("Case 2");
       break;
    }
    return 0;
}

Error: No default value is specified
Error: Constant expression required at line case P:
Error: There is no break statement in each case.
No error will be reported.
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int i = 0;
    i++;
    if(i <= 5)
    {
        printf("ASTIWZ");
        exit();
        main();
    }
    return 0;
}

The program prints 'ASTIWZ' 5 times
The program prints 'ASTIWZ' one time
The call to main() after exit() doesn't materialize.
The compiler reports an error since main() cannot call itself.
What will be the output of the program?
#include<stdio.h>
int main()
{
    char ch;
    if(ch = printf(""))
        printf("It matters");
    else
        printf("It doesn't matters");
    return 0;
}

It matters
It doesn't matters
matters
No output
Point out the error, if any in the program.
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        case 1:
           printf("Case1");
           break;
        case 1*2+4:
           printf("Case2");
           break;
    }
return 0;
}

Error: in case 1*2+4 statement
Error: No default specified
Error: in switch statement
No Error
Which of the following statements are correct about the below C-program?
#include<stdio.h>
int main()
{
    int x = 10, y = 100%90, i;
    for(i=1; i<10; i++)
    if(x != y);
        printf("x = %d y = %d", x, y);
    return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.

1
2, 3
3, 4
4
What will be the output of the program?
#include<stdio.h>
int main()
{
    unsigned int i = 65536; /* Assume 2 byte integer*/
    while(i != 0)
        printf("%d",++i);
    printf("");
    return 0;
}

Infinite loop
0 1 2 ... 65535
0 1 2 ... 32767 - 32766 -32765 -1 0
No output
Point out the error, if any in the while loop.
#include<stdio.h>
int main()
{
    void fun();
    int i = 1;
    while(i <= 5)
    {
        printf("%d", i);
        if(i>2)
            goto here;
    }
return 0;
}
void fun()
{
    here:
    printf("It works");
}

No Error: prints "It works"
Error: fun() cannot be accessed
Error: goto cannot takeover control to other function
No error
Which of the following sentences are correct about a for loop in a C program?
1: for loop works faster than a while loop.
2: All things that can be done using a for loop can also be done using a while loop.
3: for(;;); implements an infinite loop.
4: for loop can be used if we want statements in a loop get executed at least once.

1
1, 2
2, 3
2, 3, 4
What will be the output of the program?
#include<stdio.h>
int main()
{
    float a = 0.7;
    if(0.7 > a)
        printf("Hi");
    else
        printf("Hello");
    return 0;
}

Hi
Hello
Hi Hello
None of above
Point out the error, if any in the program.
#include<stdio.h> 
int main()
{
    int a = 10, b;
    a >=5 ? b=100: b=200;
    printf("%d", b);
    return 0;
}

100
200
Error: L value required for b
Garbage value
Which of the following statements are correct about the below program?
#include<stdio.h>
int main()
{
    int n = 0, y = 1;
    y == 1 ? n=0 : n=1;
    if(n)
        printf("Yes");
    else
        printf("No");
    return 0;
}

Error: Declaration terminated incorrectly
Error: Syntax error
Error: Lvalue required
None of above
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a=0, b=1, c=3;
    *((a) ? &b : &a) = a ? b : c;
    printf("%d, %d, %d", a, b, c);
    return 0;
}

0, 1, 3
1, 2, 3
3, 1, 3
1, 3, 1
Which of the following sentences are correct about a switch loop in a C program?
1: switch is useful when we wish to check the value of variable against a particular set of values.
2: switch is useful when we wish to check whether a value falls in different ranges.
3: Compiler implements a jump table for cases used in switch.
4: It is not necessary to use a break in every switch statement.

1,2
1,3,4
2,4
2
What will be the output of the program?
#include<stdio.h>
int main()
{
    int k, num = 30;
    k = (num < 10) ? 100 : 200;
    printf("%d", num);
    return 0;
}

200
30
100
500
What will be the output of the program?
#include<stdio.h>
int main()
{
    int a = 300, b, c;
    if(a >= 400)
        b = 300;
    c = 200;
    printf("%d, %d, %d", a, b, c);
    return 0;
}

300, 300, 200
Garbage, 300, 200
300, Garbage, 200
300, 300, Garbage
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x=1, y=1;
    for(; y; printf("%d %d", x, y))
    {
        y = x++ <= 5;
    }
    printf("");
    return 0;
}

2 1
3 1
4 1
5 1
6 1
7 0
2 1
3 1
4 1
5 1
6 1
2 1
3 1
4 1
5 1
2 2
3 3
4 4
5 5
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i = 5;
    while(i-- >= 0)
        printf("%d,", i);
    i = 5;
    printf("");
    while(i-- >= 0)
        printf("%i,", i);
    while(i-- >= 0)
        printf("%d,", i);
    return 0;
}

4, 3, 2, 1, 0, -1
4, 3, 2, 1, 0, -1
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
Error
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=3;
    switch(i)
    {
        case 1:
            printf("Hello");
        case 2:
            printf("Hi");
        case 3:
            continue;
        default:
            printf("Bye");
    }
    return 0;
}

Error: Misplaced continue
Bye
No output
Hello Hi
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x = 10, y = 20;
    if(!(!x) && x)
        printf("x = %d", x);
    else
        printf("y = %d", y);
    return 0;
}

y =20
x = 0
x = 10
x = 1
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default");
        case 1:
           printf("This is case 1");
           break;
        case 2:
           printf("This is case 2");
           break;
        case 3:
           printf("This is case 3");
    }
    return 0;
}

This is default
This is case 1
This is case 3
This is default
This is case 1
This is case 3
This is default
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i = 1;
    switch(i)
    {
        printf("Hello");
        case 1:
            printf("Hi");
            break;
        case 2:
            printf("Bye");
            break;
    }
    return 0;
}

Hello
Hi
Hello
Bye
Hi
Bye
What will be the output of the program?
#include<stdio.h>
int main()
{
    char j=1;
    while(j < 5)
    {
        printf("%d, ", j);
        j = j+1;
    }
    printf("");
    return 0;
}

1 2 3 ... 127
1 2 3 ... 255
1 2 3 ... 127 128 0 1 2 3 ... infinite times
1, 2, 3, 4
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x, y, z;
    x=y=z=1;
    z = ++x || ++y && ++z;
    printf("x=%d, y=%d, z=%d", x, y, z);
    return 0;
}

x=2, y=1, z=1
x=2, y=2, z=1
x=2, y=2, z=2
x=1, y=2, z=1