Sei sulla pagina 1di 19

1. C99 standard guarantees uniqueness of __________ characters for internal names.

a) 31
b) 63
c) 12
d) 14
View Answer
Answer: b
Explanation: ISO C99 compiler may consider only first 63 characters for internal names.
2. C99 standard guarantees uniqueness of ___________ characters for external names.
a) 31
b) 6
c) 12
d) 14
View Answer
Answer: a
Explanation: ISO C99 compiler may consider only first 31 characters for external names.
3. Which of the following is not a valid variable name declaration?
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned
View Answer
Answer: d
Explanation: None.
4. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a
View Answer
Answer: c
Explanation: Variable name cannot start with a digit.
5. Why do variable names beginning with the underscore is not encouraged?
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system
View Answer
Answer: c
Explanation: None.
6. All keywords in C are in ____________
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
7. Variable name resolution (number of significant characters for the uniqueness of variable) depends
on ___________
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None of the mentioned
View Answer
Answer: a
Explanation: It depends on the standard to which compiler and linkers are adhering.
advertisement

8. Which of the following is not a valid C variable name?


a) int number;
b) float rate;
c) int variable_count;
d) int $main;
View Answer
Answer: d
Explanation: Since only underscore and no other special character is allowed in a variable name, it
results in an error.
9. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
View Answer
Answer: c
Explanation: According to the syntax for C variable name, it cannot start with a digit.
1. Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
View Answer
Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello World! %d \n", x);
5. return 0;
6. }

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
Answer: c
Explanation: It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)
3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
View Answer
Answer: a
Explanation: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here
4. Which of the following is not a valid variable name declaration?
a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14
View Answer
Answer: d
Explanation: #define PI 3.14 is a macro preprocessor, it is a textual substitution.
5. What will happen if the following C code is executed?

1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }

a) It will cause a compile-time error


b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
View Answer
Answer: c
Explanation: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3
6. What is the problem in following variable declaration?
float 3Bedroom-Hall-Kitchen?;

advertisement

a) The variable name begins with an integer


b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned
View Answer
Answer: d
Explanation: A variable name cannot start with an integer, along with that the C compiler interprets the
‘-‘ and ‘?’ as a minus operator and a question mark operator respectively.
7. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int ThisIsVariableName = 12;
5. int ThisIsVariablename = 14;
6. printf("%d", ThisIsVariablename);
7. return 0;
8. }

a) The program will print 12


b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration
View Answer
Answer: b
Explanation: Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is
case sensitive.
Output:
$ cc pgm4.c
$ a.out
14
8. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export
View Answer
Answer: a
Explanation: volatile is C keyword.
1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int a[5] = {1, 2, 3, 4, 5};
5. int i;
6. for (i = 0; i < 5; i++)
7. if ((char)a[i] == '5')
8. printf("%d\n", a[i]);
9. else
10. printf("FAIL\n");
11. }

a) The compiler will flag an error


b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times
View Answer
Answer: d
Explanation: The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAIL
FAIL
FAIL
FAIL
FAIL
2. The format identifier ‘%i’ is also used for _____ data type.
a) char
b) int
c) float
d) double
View Answer
Answer: b
Explanation: Both %d and %i can be used as a format identifier for int data type.
3. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short
b) unsigned short
c) long
d) int
View Answer
Answer: b
Explanation: 65000 comes in the range of short (16-bit) which occupies the least memory. Signed
short ranges from -32768 to 32767 and hence we should use unsigned short.
4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
View Answer
Answer: d
Explanation: typedef and struct are used to define user-defined data types.
5. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
View Answer
Answer: c
Explanation: The size of the data types depend on the system.
6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. signed char chr;
5. chr = 128;
6. printf("%d\n", chr);
7. return 0;
8. }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
View Answer
Answer: b
Explanation: signed char will be a negative number.
Output:
$ cc pgm2.c
$ a.out
-128
7. What will be the output of the following C code?
advertisement

1. #include <stdio.h>
2. int main()
3. {
4. char c;
5. int i = 0;
6. FILE *file;
7. file = fopen("test.txt", "w+");
8. fprintf(file, "%c", 'a');
9. fprintf(file, "%c", -1);
10. fprintf(file, "%c", 'b');
11. fclose(file);
12. file = fopen("test.txt", "r");
13. while ((c = fgetc(file)) != -1)
14. printf("%c", c);
15. return 0;
16. }

a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
View Answer
Answer: a
Explanation: None.
Output:
$ cc pgm3.c
$ a.out
a
8. What is short int in C programming?
a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic data type
d) All of the mentioned
View Answer
Answer: c
Explanation: None.
1. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }

a) equal
b) not equal
c) output depends on the compiler
d) error
View Answer
Answer: b
Explanation: 0.1 by default is of type double which has different representation than float resulting in
inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1f)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }

a) equal
b) not equal
c) output depends on compiler
d) error
View Answer
Answer: a
Explanation: 0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
3. What will be the output of the following C code on a 32-bit machine?

1. #include <stdio.h>
2. int main()
3. {
4. int x = 10000;
5. double y = 56;
6. int *p = &x;
7. double *q = &y;
8. printf("p and q are %d and %d", sizeof(p), sizeof(q));
9. return 0;
10. }

a) p and q are 4 and 4


b) p and q are 4 and 8
c) compiler error
d) p and q are 2 and 8
View Answer
Answer: a
Explanation: Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4
4. Which is correct with respect to the size of the data types?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
View Answer
Answer: c
Explanation: char has less bytes than int and int has less bytes than double in any system
5. What will be the output of the following C code on a 64 bit machine?

1. #include <stdio.h>
2. union Sti
3. {
4. int nu;
5. char m;
6. };
7. int main()
8. {
9. union Sti s;
10. printf("%d", sizeof(s));
11. return 0;
12. }

a) 8
b) 5
c) 9
d) 4
View Answer
Answer: d
Explanation: Since the size of a union is the size of its maximum data type, here int is the largest data
type. Hence the size of the union is 4.
Output:
$ cc pgm7.c
$ a.out
4
advertisement

6. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. float x = 'a';
5. printf("%f", x);
6. return 0;
7. }

a) a
b) run time error
c) a.0000000
d) 97.000000
View Answer
Answer: d
Explanation: Since the ASCII value of a is 97, the same is assigned to the float variable and printed.
Output:
$ cc pgm8.c
$ a.out
97.000000
7. Which of the data types has the size that is variable?
a) int
b) struct
c) float
d) double
View Answer
Answer: b
Explanation: Since the size of the structure depends on its fields, it has a variable size.
. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
5. printf("PEACH = %d\n", PEACH);
6. }

a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6
View Answer
Answer: c
Explanation: In enum, the value of constant is defined to the recent assignment from left.
Output:
$ cc pgm1.c
$ a.out
PEACH = 5
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
5. }

a)

C programming Class by

WOW Sanfoundry
b) C programming Class by\n%s Sanfoundry
c)

C programming Class by

%s Sanfoundry

d) Compilation error
View Answer
Answer: c
Explanation: This program has only one %s within first double quotes, so it does not read the string
“WOW”.
The %s along with the Sanfoundry is not read as a format modifier while new line character prints the
new line.
Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry
3. In the following code snippet, character pointer str holds a reference to the string ___________
char *str = "Sanfoundry.com\0" "training classes";

a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration
View Answer
Answer: b
Explanation: ‘\0’ is accepted as a char in the string. Even though strlen will give length of string
“Sanfoundry.com”, in memory str is pointing to entire string including training classes.
4. What will be the output of the following C code?

1. #include <stdio.h>
2. #define a 10
3. int main()
4. {
5. const int a = 5;
6. printf("a = %d\n", a);
7. }

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
View Answer
Answer: c
Explanation: The #define substitutes a with 10 without leaving any identifier, which results in
Compilation error.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant
5. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. int var = 010;
5. printf("%d", var);
6. }

a) 2
b) 8
c) 9
d) 10
View Answer
Answer: b
Explanation: 010 is octal representation of 8.
Output:
$ cc pgm4.c
$ a.out
8
advertisement

6. What will be the output of the following C function?

1. #include <stdio.h>
2. enum birds {SPARROW, PEACOCK, PARROT};
3. enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
4. int main()
5. {
6. enum birds m = TIGER;
7. int k;
8. k = m;
9. printf("%d\n", k);
10. return 0;
11. }
a) 0
b) Compile time error
c) 1
d) 8
View Answer
Answer: d
Explanation: m is an integer constant, hence it is compatible.
Output:
$ cc pgm5.c
$ a.out
8
7. What will be the output of the following C code?

1. #include <stdio.h>
2. #define MAX 2
3. enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
4. int main()
5. {
6. enum bird b = PARROT;
7. printf("%d\n", b);
8. return 0;
9. }

a) Compilation error
b) 5
c) Undefined value
d) 2
View Answer
Answer: b
Explanation: MAX value is 2 and hence PARROT will have value 3 + 2.
Output:
$ cc pgm6.c
$ a.out
5
8. What will be the output of the following C code?

1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char *str = "x";
6. char c = 'x';
7. char ary[1];
8. ary[0] = c;
9. printf("%d %d", strlen(str), strlen(ary));
10. return 0;
11. }

a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)
View Answer
Answer: d
Explanation: str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
15
1. enum types are processed by _________
a) Compiler
b) Preprocessor
c) Linker
d) Assembler
View Answer
Answer: a
Explanation: None.
2. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("sanfoundry\rclass\n");
5. return 0;
6. }

a) sanfoundryclass
b)

sanfoundry

class
c) classundry
d) sanfoundry
View Answer
Answer: c
Explanation: r is carriage return and moves the cursor back. sanfo is replaced by class.
Output:
$ cc pgm8.c
$ a.out
classundry
3. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. printf("sanfoundry\r\nclass\n");
5. return 0;
6. }

a) sanfoundryclass
b)

sanfoundry

class

c) classundry
d) sanfoundry
View Answer
Answer: b
Explanation: rn combination makes the cursor move to the next line.
Output:
$ cc pgm9.c
$ a.out
sanfoundry
class
4. What will be the output of the following C code?

1. #include <stdio.h>
2. int main()
3. {
4. const int p;
5. p = 4;
6. printf("p is %d", p);
7. return 0;
8. }

a) p is 4
b) Compile time error
c) Run time error
d) p is followed by a garbage value
View Answer
Answer: b
Explanation: Since the constant variable has to be declared and defined at the same time, not doing
it results in an error.
Output:
$ cc pgm10.c
pgm10.c: In function ‘main’:
pgm10.c:5: error: assignment of read-only variable ‘p’

Not understand
1. What will be the output of the following C code?

1. #include <stdio.h>
2. void main()
3. {
4. int x = 0;
5. if (x = 0)
6. printf("Its zero\n");
7. else
8. printf("Its not zero\n");
9. }

a) Its not zero


b) Its zero
c) Run time error
d) None
View Answer
Answer: a
1. #include <stdio.h>
2. void main()
3. {
4. int b = 6;
5. int c = 7;
6. int a = ++b + c--;
7. printf("%d", a);
8. }

a) Run time error


b) 15
c) 13
d) 14
View Answer
Answer: d
1. #include <stdio.h>
2. void main()
3. {
4. double b = 5 % 3 & 4 + 5 * 6;
5. printf("%lf", b);
6. }

a) 2
b) 30
c) 2.000000
d) Run time error
View Answer
Answer: c

Potrebbero piacerti anche