0% found this document useful (0 votes)
985 views92 pages

C Programming Questions

The output will be: x = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 10, y = 20.000000, z = 100 Explanation: - Variables declared inside blocks overwrite variables of the same name from outer scopes. - The innermost declaration of a variable takes precedence. So in the nested blocks, the variables x, y and z refer to the innermost declarations, shadowing the outer declarations. The outer z is still accessible in the second printf.

Uploaded by

Abhishek P Nair
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
985 views92 pages

C Programming Questions

The output will be: x = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 10, y = 20.000000, z = 100 Explanation: - Variables declared inside blocks overwrite variables of the same name from outer scopes. - The innermost declaration of a variable takes precedence. So in the nested blocks, the variables x, y and z refer to the innermost declarations, shadowing the outer declarations. The outer z is still accessible in the second printf.

Uploaded by

Abhishek P Nair
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 92

C Programming

Thara. R. J
04 – 08 – 2018
GATE-2000

Find the number of tokens in the following C


statement.

printf("i = %d, &i = %x", i, &i);

(A) 3 (B) 26 (C) 10 (D) 21


Which is the valid identifier (variable
name) to store student’s age?
1. int student-age;
2. int student_age;
3. int -age;
4. int _age;
Which of the following variable
declarations and definitions is/ are correct ?
i) int var_9 = 1;
ii) int 9_var = 2;
iii) int _ = 3;
Choose the correct statement w.r.t. above
variables.
Which statement does not require
semicolon?
1. goto xyz
2. int x=20
3. #define MAX 1000
4. do{ ... }while(count<=100)
Unsigned Octal number for integer:%o

#include <stdio.h>
int main()
{
int a = 67;
printf("%o\n", a);
return 0;
}
Unsigned Hexadecimal for integer : %x, %X

#include <stdio.h>
int main()
{
int a = 15;
printf("%x\n", a);
return 0;
}
Predict output ?

int main() {
int x = 032;
printf("%d", x);
return 0;
}
Predict output ?
#include <stdio.h>
int main(){
int a,b,c;
a=0x10; b=010;
c=a+b;
printf("\nAddition is= %d",c);
return 0;
}
%d Vs %i
 %d specifies signed decimal integer while %i specifies integer.
 There is no difference between the %i and %d format specifiers for printf.
 %d and %i behavior is different in scanf
 %d assume base 10 while %i auto detects the base.
 So with scanf, 012 would be 10 with %i but 12 with %d.
 %d takes integer value as signed decimal integer i.e. it takes negative values
along with positive values but values should be in decimal otherwise it will
print garbage value.
 %i takes integer value as integer value with decimal, hexadecimal or octal type.

Note: To enter a value in hexadecimal format – value should be provided by


preceding “0x” and value in octal format – value should be provided by
preceding “0”.
C program to demonstrate difference
between %i and %d specifier
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter value of a in decimal format:"); //12
scanf("%d", &a);
printf("Enter value of b in octal format: "); //012
scanf("%i", &b);
printf("Enter value of c in hexadecimal format: "); //0x12
scanf("%i", &c);
printf("a = %i, b = %i, c = %i", a, b, c);
return 0;
}
Predict output ?

#include <stdio.h>
int main( ) {
int a;
printf("Enter number: ");
scanf("%5d", &a); //123456
printf("Formatted number is: %7d", a);
return 0;
}
Predict output ?

#include <stdio.h>
int main() {
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f",
(9/5)*c + 32);
return 0;
}
Predict output ?

#include <stdio.h>
int main() {
int data = 29;
printf("%x\n", data);
int data printf("%0x\n", data);
printf("%8x\n", data);
printf("%08x\n", data);
}
Consider the following C program
If entered data items are,
9876 5432 1
What will be the output?
#include<stdio.h>
void main()
{ int a, b, c;
printf ("Enter the numbers");
scanf("%3d%3d%3d",&a,&b,&c);
printf("%d%d%d", a, b, c);
}
Predict output ?

#include <stdio.h>
void main(){
unsigned char c=290;
printf("%d",c);
}
Predict output ?

#include <stdio.h>
void main()
{ unsigned char c=325;
printf("%c",c);
}
Predict output ?

#include<stdio.h>
int main() {
typedef int i;
i a = 0;
printf("%d", a);
return 0;
}
Predict output ?
#include <stdio.h>
enum numbers {
zero, one, two, three ,
four=3,five,six,seven=0,eight
};
void main() {
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d",zero,
one,two,three,four,five,six,seven,eight);
}
Arrange the operators according to
their precedence: +, %, ->, =
A. ->, %, +, =
B. =, +, %, ->
C. %, +, =, ->
D. %, ->, =, +
Predict output ?

#include <stdio.h>
void main()
{
printf("value is = %d",(10++));
}
Predict output ?
What will be the output of following program?
#include <stdio.h>
void main() {
unsigned short var='B';
var+=2;
var++;
printf("var : %c , %d ", var,var);
}
Answer = var : E,69
ASCII Code of
Var=‘B’ • A=65
 Var = 66 in decimal • B=66
 Var+2 = 68 (represents • C=67
‘D’ in ASCII)
• ……..
 Var++ = 69(represents
• ……..
‘E’ in ASCII)
• a = 97
 Ans = var : E,69
• b = 98
• c = 99
GATE 2017 – Set 2
Consider the following C program.
#include<stdio.h>
int main () {
int m=10;
int n, n1;
n=++m;
n1=m++;
n--;
--n1;
n-=n1;
printf(“%d”, n);
return 0;
}
The output of the program is ______
Answer = 0
• m = 10
• n = ++m
(m = 11 , n = 11)
• n1 = m++
(n1 = 11 , m = 12)
• n-- = 10
• --n1 = 10
• n -= n1
(n=0)
Predict output ?

What will be the output of following program?


#include <stdio.h>
void main()
{
int a=10,b=2,x=0;
x=a+b*a+10/2*a;
printf("value is =%d",x);
}
Answer = 80
• a=10, b=2, x=0
Applying operator precedence (left to right)
• x=a+b*a+10/2*a;
• x=a+20+10/2*a;
• x=a+20+5*a;
• x = 10+20+50
• x = 80
Predict output ?
#include <stdio.h>
void main()
{
int x=10;
x+=(x++)+(++x)+x;
printf("%d", x);
}
Answer = 80
• x=10
Applying operator precedence
• x+=(x++)+ (++x) +x;
• x+= (x++) +(11)+x; x=11
• x+=(11)+(11)+11; since x=11;
post incrementing pending
• x = 11+11+11+11+1
• x = 45
Predict output ?

#include <stdio.h>
void main()
{
int x=(20 || 40 ) && (10);
printf("x= %d",x);
}
Answer: x = 1
• X = (20 logical OR 40) logical AND 10
• X= (TRUE) AND 10
• X= TRUE
• Which is equivalent to 1
Predict output ?
#include <stdio.h>
void main() {
int x;
x= (printf("AA")||printf("BB"));
printf("%d",x);
printf("\n");
x= (printf("AA")&&printf("BB"));
printf("%d",x);
}
Answer: AA1
AABB1
• Prints AA, then the condition is logical OR. Since
AA is 1, due to optimisation – x is predicted as 1.
• Prints AA, but AA = 1. Since next condition is
logical AND, it prints BB. BB = 1. and finally
prints 1
Predict output ?
#include <stdio.h>
void main()
{
int a=3,b=2;
a=a==b==0;
printf("%d,%d",a,b);
}
Answer: 1,2
Compare from left then assign value
A= (A==B) ==0
A= (0==0)
A=1
But B= 2
Predict output ?

#include <stdio.h>
void main(){
int intVar=20,x;
x= ++intVar,intVar++,++intVar;
printf("Value of intVar=%d, x=%d",intVar,x);
}
Answer: Value of intVar= 23, 21
intVar = 20
step by step execution because of coma
step 1: ++intVar =21, x=21
step 2: intVar++ =22
step 3: ++intVar =23
Predict output ?

#include <stdio.h>
int main() {
int x,y;
x=(100,200);
y=100,200;
printf("x=%d,y=%d",x,y);
return 0;
}
Answer: 200,100
x=(100,200);
selects x=200 (because of bracket)
y=100,200;
selects y=100
Predict output ?
#include <stdio.h>
int main(){
int x;
x=100,30,50;
printf("x=%d\n",x);
x=(100,30,50);
printf("x=%d\n",x);
return 0;
}
Answer: 100,50
x=100,30,50;
selects x=100
x=(100,30,50);
selects x=50 (because of bracket)
Predict output ?

#include <stdio.h>
int main(){
char val=250;
int ans;
ans= val+ !val + ~val + ++val;
printf("%d",ans);
return 0;
}
Answer: -6
val = 250 (which is unsigned int)
val = 250-128= 122 = 1111010 (which is
2’s complement of 6)
therefore val =-6
ans = val+ !val + ~val + ++val;
-6 = -5 + 0 + 4 + -5
Predict output ?
#include <stdio.h>
int main(){
int var;
var= - -10;
printf("value of var= %d\n",var);
var= + +10;
printf("value of var= %d\n",var);
return 0;
}
Answer: value of var = 10
value of var = 10
case 1:
var = - ( - 10) =10
case 2:
var = + ( + 10) =10
ISRO 2017
What does the following program do when the input is unsigned 16 bit integer?
#include<stdio.h>
main(){
unsigned int num;
int i;
scanf("%u", &num);
for(i=0;i<16;i++){
printf("%d", (num<<i&1<<15)?1:0);
}
}

A. It prints all even bits from num B. It prints all odd bits from num
C. It prints binary equivalent of num D. None of above
Answer: c)It prints binary equivalent of num

If we give input 4, it will print 00000000 00000100


If we give input 3, it will print 00000000 00000011 ;
If we give input 511, it will print 00000001 11111111
GATE 2014 - Set 2
Consider the function func shown below:
int func(int num) {
int count = 0;
while (num) {
count++;
num >>= 1;
}
return (count);
}

The value returned by func(435)is __________.


Answer: 9

A single right shift means divide by 2.


If keep on dividing by 2, we get:
435, 217, 108, 54, 27, 13, 6, 3, 1.
Therefore, the count is 9.
Consider the following C program,
which variable has the longest scope?
int a;
int main(){
int b;
// ..
{
int c;
}
// ..
}
Answer: a

"Scope" is just a technical term for the parts of code that


have access to a variable
Global Scope: Their scope is the entire script. Which
means that you can read and change them anywhere
in your code.
Local Scope: Their scope is the part of a script. Which
means that you can read and change them only inside
its scope of your code.
Predict output ?
int main() {
{
int var = 10;
}
{
printf("%d", var);
}
return 0;
}
Answer: Compiler Errror

var is not accessible.


The curly brackets define a block of scope. Anything
declared between curly brackets goes out of scope
after the closing bracket.
Predict output ?
#include <stdio.h>
int main() {
int x = 1, y = 2, z = 3;
printf(" x = %d, y = %d, z = %d \n", x, y, z);
{
int x = 10;
float y = 20;
printf(" x = %d, y = %f, z = %d \n", x, y, z);
{
int z = 100;
printf(" x = %d, y = %f, z = %d \n", x, y, z);
}
}
return 0;
}
Answer:

x = 1, y = 2, z = 3
x = 10, y = 20.000000, z = 3
x = 10, y = 20.000000, z = 100

Reason: Check the scopes of each variable assignment.


Predict output ?

#include <stdio.h>
extern int var = 0;
int main() {
var = 10;
printf("%d ", var);
return 0;
}
Answer:10

var = 10
Reason: Check the scopes of each variable assignment.

Note : Extern is used to make a variable global even if it


is defined in main scope.
Predict output ?

#include <stdio.h>
int var = 20;
int main() {
int var = var;
printf("%d ", var);
return 0;
}
Answer: Garbage Value

First var is declared, then value is assigned to it. As soon


as var is declared as a local variable, it hides the global
variable var.
Predict output ?
#include <stdio.h>
main() {
int i;
for(i=0;i<5;i++) {
int i=10;
printf("%d" , i);
i++;
}
return 0;
}
Answer: 1010101010

Let us divide ‘I’ as Iin (for loop) and Iout (inside for
loop) based on scope.
Loop 1: Iin = 0, Iout=10, prints Iout, increments Iout.
Loop 2: Iin = 1, Iout=10, prints Iout, increments Iout.
Loop 3: Iin = 2, Iout=10, prints Iout, increments Iout.
Loop 4: Iin = 3, Iout=10, prints Iout, increments Iout.
Loop 5: Iin = 4, Iout=10, prints Iout, increments Iout.
Predict output ?
#include <stdio.h>
int main() {
static int i=5;
if (--i) {
printf("%d ",i);
main();
}
}
Predict output ?
#include <stdio.h>
int main() {
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
What is the output of the program?

(A) 123323453 (B) 1233123453 (C) 1233123455 (D) 12332345


Predict output ?
#include <stdio.h>
int fun(int n) {
static int s = 0;
s = s + n;
return (s); }
int main() {
int i = 10, x;
while (i > 0) {
x = fun(i);
i--; }
printf ("%d ", x);
return 0;
}
Predict output ?
Predict output ?
#include <stdio.h>
int fun() {
static int num = 16;
return num--;
}
int main() {
for(fun(); fun(); fun())
printf("%d ", fun());
return 0;
}
GATE-CS-2007
#include <stdio.h>
int f(int n) {
static int r = 0;
if (n <= 0) return 1;
if (n > 3) {
r = n;
return f(n-2)+2;
}
return f(n-1)+r;
}

int main() {
printf("%d", f(5));
}
GATE CS 2004
Consider the following C function
int f(int n) {
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
GATE CS 2012 - Predict output ?
#include <stdio.h>
int a, b, c = 0;
void prtFun (void);
int main () {
static int a = 1;
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void) {
static int a = 2;
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
GATE CS 2012 - Predict output ?
#include <stdio.h>
int a, b, c = 0;
void prtFun (void);
int main () {
auto int a = 1;
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
void prtFun (void) {
register int a = 2;
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
GATE 2017 - Set 1
#include<stdio.h>
int total(int v) {
static int count = 0;
while(v) {
count += v&1;
v >>= 1; }
return count;
}
void main() {
static int x=0;
int i=5;
for(; i>0; i--) { x = x + total(i); }
printf("%d\n", x);
}
For a given integer n, what does the
function compute?

(A) Factorial of a number


(B) n^n
(C) Product of odd numbers till n (inclusive n)
(D) Product of even numbers till n (inclusive n)
GATE 2014 - Set 2
Consider the C function given below.
int f(int j) {
static int i = 50;
int k;
if (i == j) {
printf("something");
k = f(i);
return 0;
}
else return 0;
}
Which one of the following is TRUE?
(A) The function returns 0 for all values of j.
(B) The function prints the string something for all values of j.
(C) The function returns 0 when j = 50.
(D) The function will exhaust the runtime stack or run into an infinite loop when j = 50
Predict output ?
What is the value returned by f(g(3))?
What will be the output if foo(4) is called?
What is value returned when fun(90)
is called?
What is the value returned by the
function if sum (1001) is called?
GATE 2017 – Set 2
Consider the C program fragment below which is meant to divide x by y using
repeated subtractions. The variables x, y, q and r are all unsigned int.
while (r >= y) {
r=r-y;
q=q+1;
}
Which of the following conditions on the variables x,y,q and r before the
execution of the fragment will ensure that the loop terminated in a state
satisfying the condition x==(y∗q+r)?

A. (q==r) && (r==0) B. (x>0) && (r==x) && (y>0)


C. (q==0) && (r==x) && (y >0) D. (q==0) && (y>0)
GATE-CS-2014-(Set-2)
Suppose n and p are unsigned int variables in a C
program. We wish to set p to nC3. If n is large,
which of the following statements is most
likely to set p correctly?
A. p = n * (n-1) * (n-2) / 6;
B. p = n * (n-1) / 2 * (n-2) / 3;
C. p = n * (n-1) / 3 * (n-2) / 2;
D. p = n * (n-1) * (n-2) / 6.0;
Predict output ?
#include<stdio.h>
main() {
float a;
int x=6,y=5;
a=x/y;
printf ("Value of a =%f\n", a);
return 0;
}
(A) 1.200000 (B) 1.000000 (C) 1 (D)None
What is the output of the below
program if we enter “Good Morning”?
main ( ) {
char w1[50];
printf ("enter text");
scanf ("%s", w1);
printf (“%s”, w1);
}
(A) Good Morning (B) Good Morning
(C) Good (D) None of these
What is the output of the below program?
GATE 2017- Set 1
Consider the C functions foo and bar given below:
int foo(int val) { int bar(int val) {
int x=0; int x = 0;
while(val > 0) { while(val > 0) {
x = x + foo(val--); x= x + bar(val-1);
} }
return val; return val;
} }
Invocations of foo(3) and bar(3) will result in:

(A) Return of 6 and 6 respectively.


(B) Infinite loop and abnormal termination respectively.
(C) Abnormal termination and infinite loop respectively.
(D) Both terminating abnormally.
Which of the following cannot be
checked in switch-case statement?
(A) int
(B) float
(C) char
(D) enum
What is the output of following program?

(A) Gate (B) gatecse (C) 2018 (D) Compile Time Error
Predict output ?
Predict output ?
#include "stdio.h"
int * gPtr;
int main() {
int * lPtr = NULL;
if(*gPtr == *lPtr) {
printf("Equal!");
}
Else {
printf("Not Equal");
}
return 0;
}
GATE 2014 - Set 1
Consider the following program in C language:
#include <stdio.h>
main() {
int i;
int *pi = &i;
scanf("%d", pi);
printf("%d\n", i+5);
}
Which one of the following statements is TRUE?
(A) Compilation fails.
(B) Execution results in a run-time error.
(C) On execution, the value printed is 5 more than the address of variable i.
(D) On execution, the value printed is 5 more than the integer value entered.
Give output with call by value/
reference scheme
int i ;
program main () {
int j = 60;
i = 50;
call f (i, j);
print i, j;
}
procedure f (x, y) {
i = 100;
x = 10;
y=y+i;
}
Predict output ?

int main() {
char st1[10] = “India”;
char st2[10] = “India”;
if(st1 == st2)
printf(“Two string are equal”);
else
printf(“Both are unequal”);
}

You might also like