Sei sulla pagina 1di 14

Sources / sites

http://www.cquestions.com/2012/02/data-type-questions-in-c.html

http://www.indiabix.com/c-programming/pointers/

For every question you work on, observe and understand why certain outcome is unexpectedly different than what should have been a
straight forward outcome. The objective of the questions is to get you familiar with the different ways C operates.

If the code generates a compilation error, then identify the reason for the error and fix it.

Questions Lesson(s) Learned


1 What is the outcome/output of the following C Error
program, if any?

111

2 What is the outcome/output of the following C Prints all on same line, no indent
program, if any?

#include<stdio.h>
void main(void){
int a=100;
if(a>30)
printf(" Greater than 30 ");
if(a>20)
printf("Greater than 20 ");
if(a>10)
printf("Greater than 10 ");
}

3 What is the outcome/output of the following C Just prints 30 because initial condition was satisfied
program, if any?

#include<stdio.h>
void main(void){
    int a=100;
    if(a>30)
         printf(" Greater than 30 ");
    else if(a>20)
         printf("Greater than 20 ");
    else if(a>10)
           printf("Greater than 10 ");
}
4 What is the outcome/output of the following C Greater than 30 not less than 20 greater than 10
program, if any?

#include<stdio.h>
void main(void){
    int a=100;
    if(a>30)
         printf(" Greater than 30 ");
    if(a<20)
         printf("Less than 20 ");
      else 
         printf("Not less than 20 ");
    if(a>10)
           printf("Greater than 10 ");
}

5 What is the outcome/output of the following C Greater than 30 n0t less than 20
program, if any?

#include<stdio.h>
void main(void){ Greater than 30 not less than 20
int a=100;
if(a>30)
{
printf(" Greater than 30 ");
if(a<20)
printf("Less than 20 ");
else
printf("Not less than 20 ");
}
else if(a>10)
printf("Greater than 10 ");
}
6 What is the outcome/output of the following C Just greater than 10, initial condition statement not met,
program, if any? moves to else if

#include<stdio.h>
void main(void){
int a=100;
if(a < 30)
{
printf(" Less than 30 ");
if(a > 20)
printf("Greater than 20 ");
else
printf("Not greater than 20 ");
}
else if(a>10)
printf("Greater than 10 ");
}
7 What is the outcome/output of the following C Shows not equal even though values are equal??
program, if any?

#include<stdio.h>
void main(void){
int x=-1,y=-1;
if(++x = ++y)
printf(" Equal ");
else
printf("Not equal %d %d, x, y");
}

8 What is the outcome/output of the following C


program, if any?2
Some how skips warren buffet and prints first and last
#include<stdio.h> same line
void main(void){
int m=5,n=10,q=20;
if(q/n*m)
printf("William Gates");
else
printf(" Warren Buffet");
printf(" Carlos Slim Helu");
}
9 What is the outcome/output of the following C Shows printed number in print f 123456789
program, if any? Indents and says printf returned 10
10 is arbitrary
#include<stdio.h>
void main(void){
int a=10, how_many_printed;
how_many_printed = printf("123456789\n”);
if(how_many_printed)
printf(“printf returned %d\n”,
how_many_printed);
}
10 What is the outcome/output of the following C In this condition only the first variable gets added up by
program, if any? one. Condition gets met in if statement and just goes to
print f
#include<stdio.h>
void main(void){
int a=5,b=10;
if(++a||++b)
printf("%d %d",a,b);
else
printf("John Terry");
}
11 What is the outcome/output of the following C
program, if any?
#include<stdio.h>
/* note the use of ; at the end of if statement */
void main(void){
int a=0,b=0,c=10;
if(a);
printf(“a is %d\n”,a);
if(b)
printf(“b is %d\n”,b);
}

12 What is the outcome/output of the following C


program, if any?

#include<stdio.h>
void main(void){
int x=1;
if(--x)
printf("The Godfather");
--x;
else
printf("%d",x);
}
13 Which of the following statements are correct about the
below program?
#include<stdio.h>
void main(void)
{
int i =5, j = 20;
if(i = 5) && if(j = 20)
printf("Have a nice day");
}

A. Output: Have a nice day


B. No output
C. Error: Expression syntax
D. Error: Undeclared identifier if
14 Fix the errors and analyze the output for each
#include<stdio.h>
void main(void){
int a=0,b=10;
if(a=7)
printf(" a is 7");
else
printf("a is not 7");

if(10==b)
printf(" b is 10 ");
else
printf("b is not 10");

if(a=0)
printf(" a is 0");
else
printf("a is not 0");

if(7=a)
printf(" a is 7");
else
printf("a is not 7");

}
15 What is the outcome/output of the following C Not equal, I is postincrement adding 1 so &&
program, if any?
void main(void)
{
int i = 1;
if (i++ && (i == 1))
printf("Equal n");
else
printf("Not equal\n");
}
16 What is the outcome/output of the following C
program, if any?
int main(void)
{
int a = 10, b = 10;
if (a = 5)
b--;
printf("a=%d, b=%d\n", a, b--);
}
A. a = 10, b = 9
B. a = 10, b = 8
C. a = 5, b = 9
D. a = 5, b = 8
17 What is the outcome/output of the following C
program, if any?

    void main(void)
    {
        int i = 0,y;
        int x = ++i,
y = i++;
        printf("%d % d %d \n", x, y,i);
     }

18 What is the outcome/output of the following C


program, if any?
#include<stdio.h>
void main(void)
{
char j=1;
while(j < 5)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
}
19 What is the outcome/output of the following C
program, if any?
#include<stdio.h>
void main(void)
{
char j=1;
while(j < ‘5’)
{
printf("%d, ", j);
j = j+1;
}
printf("\n");
}
20 #include<stdio.h>

void main(void){

int x=4;

while(3){

do{
printf("%d",x);

} while(0);

};

}
21 What is the outcome/output of the following C ** might have to wait until we cover break and continue
program, if any?

#include<stdio.h>
void main(void)
{
int i=1;
while()
{
printf("%d\n", i++);
if(i>10)
break;
}
}
A. There should be a condition in the while loop
B. There should be at least a semicolon in the while
C. The while loop should be replaced with for loop.
D. All the above
E. No error

22 What is the outcome/output of the following C


program, if any?

#include <stdio.h>
int main(void)
{
int x = 2, y = 2;
x /= x / y;
printf("%d\n", x);
return 0;
}
23 What is the outcome/output of the following C Try this on any online compiler and compare output
program, if any?

#include<stdio.h> If your output is different, then your compiler is not


void main(void){
following the precedence chart.
int i=1,x=2;
i=2+x*i++;
printf("%d ",i);
Visual Studio apparently does not follow the precedence
i *= x + i; chart.
printf("%d ",i);

Potrebbero piacerti anche