Monday, November 23, 2015

Variable naming rule questions in c

Following question are based on variable naming rules in c. If you have any doubt in the variable naming rules you can ask here.

Rules for identifiers in c language


1.

What will be output of the following c program?

#include<stdio.h>
int main(){
int goto=5;
printf("%d",goto);
return 0;
}

(A)5
(B)**
(C)**
(D)Compilation error
(E)None of these


Explanation:

Invalid variable name. goto is keyword in c. variable name cannot be any keyword of c language.

2.

What will be output of the following c program?

#include<stdio.h>
int main(){
long int 1a=5l;
printf("%ld",1a);
    return 0;
}

(A)5
(B)51
(C)6
(D)Compilation error
(E)None of these


Explanation:

Invalid variable name. Variable name must star from either alphabet or underscore.

3.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int _=5;
    int __=10;
    int ___;
    ___=_+__;
    printf("%i",___);
    return 0;
}

(A)5
(B)10
(C)15
(D)Compilation error
(E)None of these

Explanation:

Variable name can have only underscore.

4.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int max-val=100;
    int min-val=10;
    int avg-val;
    avg-val = max-val + min-val / 2;
    printf("%d",avg-val);
    return 0;
}

(A)55
(B)105
(C)
60
(D)Compilation error
(E)None of these


Explanation:

We cannot use special character – in the variable name.

5.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int class=150;
    int public=25;
    int private=30;
    class = class >> private - public;
    printf("%d",class);
    return 0;
}

(A)1
(B)2
(C)4
(D)Compilation error
(E)None of these


Explanation:

Variable name can be keyword of c++.

6.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int abcdefghijklmnopqrstuvwxyz123456789=10;
    int abcdefghijklmnopqrstuvwxyz123456=40;
    printf("%d",abcdefghijklmnopqrstuvwxyz123456);
    return 0;
}


(A)10
(B)40
(C)50
(D)Compilation error
(E)None of these


Explanation:

Only first 32 characters are significant in variable name. So compiler will show error: Multiple declaration of identifier abcdefghijklmnopqrstuvwxyz123456.


7.

What will be output of the following c program?

#include<stdio.h>
int main(){
    register xyz_123=91;
    auto pqr_123=991;
    const _1a1_=pqr_123+~xyz_123;
    printf("%d",_1a1_);
    return 0;
}

(A)900
(B)999
(C)899
(D)Compilation error
(E)None of these


Explanation:

Variable name can have digits.

8.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int __SMALL__ = 11;
    int y;
    y= __SMALL__ < 5;
    printf("%d",y);
    return 0;
}

(A)11
(B)5
(C)0
(D)Compilation error
(E)None of these


Explanation:

Variable name cannot be global identifier.

9.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int __BIG__ = 32;
    int y;
    y= __BIG__ && 8;
    printf("%d",y);
    return 0;
}

(A)
32
(B)8
(C)1
(D)Compilation error
(E)None of these


Explanation:

Variable name can be in the format: __NAME__. But it is bad practice since this format is used for global identifiers.


10.

What will be output of the following c program?

#include<stdio.h>
static num=5;
int num;
extern int num;
int main(){
    printf("%d",num);
    return 0;
}

(A)5
(B)10
(C)0
(D)Compilation error
(E)None of these


Explanation:

Two or more global variables can have same name but we can initialize only one of them.


11.

What will be output of the following c program?

#include<stdio.h>
static num=5;
extern int num;
int main(){
    printf("%d",num);
    return 0;
}
int num =25;

(A)
0
(B)5
(C)25
(D)Compilation error
(E)None of these


Explanation:

Two or more global variables can have same name but we can initialize only one of them.


12.

What will be output of the following c program?

#include<stdio.h>
static num;
int main(){
    printf("%d",num);
    return 0;
}
int num =25;

(A)0
(B)1
(C)25
(D)Compilation error
(E)None of these


Explanation:

Two or more global variables can have same name but we can initialize only one of them.

13.

What will be output of the following c program?

#include<stdio.h>
int xyz=10;
int main(){
    int xyz=20;
    printf("%d",xyz);
    return 0;
}

(A)10
(B)20
(C)30
(D)Compilation error
(E)None of these


Explanation:

Two variables can have same name in different scope.

14.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int xyz=20;
    int xyz;
    printf("%d",xyz);
    return 0;
}

(A)20
(B)
0
(C)Garbage
(D)Compilation error
(E)None of these


Explanation:

Two local variables cannot have same name in same scope.

15.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int xyz=20;{
         int xyz=40;
    }
    printf("%d",xyz);
    return 0;
}

(A)20
(B)
40
(C)
0
(D)Compilation error
(E)None of these


Explanation:

Two variables can have same name in different scope.

16.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int main = 80;
    printf("%d",main);
    return 0;
}

(A)80
(B)
0
(C)
Garbage value
(D)Compilation error
(E)None of these


Explanation:

Variable name can be main.

17.

What will be output of the following c program?

#include<stdio.h>
int main(){
    struct a{
         int a;
    };
    struct a b={10};
    printf("%d",b.a);
    return 0;
}

(A)0
(B)
10
(C)Garbage value
(D)Compilation error
(E)None of these


Explanation:

Two variables can have same name in different scope.

18.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int ABC=10;
    printf("%d",abc);
    return 0;
}

(A)10
(B)0
(C)5
(D)Compilation error
(E)None of these


Explanation:

Variable name is case sensitive.

19.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int printf=12;
    printf("%d",printf);
    return 0;
}

(A)12
(B)
0
(C)5
(D)Compilation error
(E)None of these


Explanation:

Variable name cannot be pre defined function of included header file.

20.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int EOF=12;
    printf("%d",EOF);
    return 0;
}

(A)12
(B)
0
(C)5
(D)Compilation error
(E)None of these


Explanation:

Variable name cannot be pre defined function of included header file.

Printf objective types interview questions and answers

Printf objective types interview questions and answers  

(1)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}

What will output when you compile and run the above code?

(a)Garbage value garbage value garbage value
(b)5 6 11
(c)11 6 5
(d)Compiler error

Answer: (c)
(2)

#include<stdio.h>

void main()
{
char *str="CQUESTIONBANK";
clrscr();
printf(str+9);
getch();
}

What will output when you compile and run the above code?

(a)CQESTIONBANK
(b)CQUESTION
(c)BANK
(d)Compiler error


Answer: (c)
(3)

#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("CQUESTIONBANK"));
getch();
}

What will output when you compile and run the above code?

(a)13CQUESTIONBANK
(b)CQUESTIONBANK13
(c)Garbage CQUESTIONBANK
(d)Compiler error


Answer: (b)

(4)
#include<stdio.h>
#include<conio.h>
void main()
{
short int a=5;
clrscr();
printf("%d"+1,a);
getch();
}

What will output when you compile and run the above code?
(a)6
(b)51
(c)d
(d)Compiler error


Answer: (c)

(5)
#include<stdio.h>
void main()
{
int i=85;
clrscr();
printf("%p %Fp",i,i);
getch();
}

What will output when you compile and run the above code?

(a)85 85
(b)0055 034E:0055
(c)0055 FFFF:0055
(d)Compiler error


Answer: (b)
 (6)
#include<stdio.h>
static struct student
{
int a;
    int b;
    int c;
int d;
}s1={6,7,8,9},s2={4,3,2,1},s3;
void main()
{
s3=s1+s2;
clrscr();
printf("%d %d %d %d",s3.a,s3.b,s3.c,s3.d);
getch();
}
What will output when you compile and run the above code?
(a)6789
(b)4321
(c)10101010
(d)Compiler error


Answer: (d)

(7)
#include<stdio.h>
extern struct student
{
int a;
    int b;
    int c;
int d;
}s={6,7,8,9};

void main()
{
clrscr();
printf("%d %d %d %d",s.a,s.b,s.c,s.d);
getch();
}

What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error


Answer: (a)

(8)
#include<stdio.h>
struct student
{
static int a;
register int b;
auto int c;
extern int d;
}s={6,7,8,9};
void main()
{
printf("%d %d % %d",s.a,s.b,s.c,s.d);
}

What will output when you compile and run the above code?
(a)6789
(b)9876
(c)0000
(d)Compiler error


Answer: (d)

(9)
#include<stdio.h>
struct student
{
int roll;
int cgpa;
int sgpa[8];
};
void main()
{
struct student s={12,8,7,2,5,9};
int *ptr;
ptr=(int *)&s;
clrscr();
printf("%d",*(ptr+3));
getch();
}
What will output when you compile and run the above code?
(a)8
(b)7
(c)2
(d)Compiler error


Answer: (c)
(10)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2={"anil"};
}g3={10,200};
void main()
{
struct game g1=g3;
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}

What will output when you compile and run the above code?
(a)10 200 anil
(b)200 10 anil
(c)10 200 null
(d)Compiler error


Answer: (d)

(11)
#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2;
}g1;
void main()
{
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}

What will output when you compile and run the above code?
(a)Garbage_value garbage_value garbage_value
(b)0 0 (null)
(c)Run time error
(d)Compiler error

 Answer: (b)