C Programming Multiple Choice Question - Variables And Datatypes
This section focuses on the "Variables And Datatypes" of the C programming. These Multiple Choice Questions (mcq) should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. How many keywords are there in c ?
A. 31
B. 32
C. 64
D. 63
View Answer
Ans : B
Explanation: There are total 32 keywords in C.Keywords are those words whose meaning is already defined by Compiler.C Keywords are also called as Reserved words.
2. Which of the following is true for variable names in C?
A. Variable names cannot start with a digit
B. Variable can be of any length
C. They can contain alphanumeric characters as well as special characters
D. Reserved Word can be used as variable name
View Answer
Ans : A
Explanation: Variable names cannot start with a digit in C Programming language.
3. Which of the following cannot be a variable name in C?
A. TRUE
B. friend
C. export
D. volatile
View Answer
Ans : D
Explanation: volatile is C keyword.Volatile in C programming language signify that the compiler that the software in hand (the thread for the routine it’s compiling) doesn’t have exclusive control over the variable described as "volatile"
4. What is the output of this program?
void main()
{
int x = 10;
float x = 10;
printf("%d", x)
}
A. Compilations Error
B. 10
C. 10
D. 10.1
View Answer
Ans : A
Explanation: Since the variable x is defined both as integer and as float, it results in an error.
5. What is the output of this program?
#include <stdio.h>
int main()
{
int i;
for (i = 0;i < 5; i++)
int a = i;
printf("%d", a);
}
A. Syntax error in declaration of a
B. No errors, program will show the output 5
C. Redeclaration of a in same scope throws error
D. a is out of scope when printf is called
View Answer
Ans : A
Explanation: the output of this program is the Syntax error in declaration of variable a.
6. What is the output of this program?
#include <stdio.h>
int var = 20;
int main()
{
int var = var;
printf("%d ", var);
return 0;
}
A. Garbage Value
B. 20
C. Compiler Error
D. None of these
View Answer
Ans : A
Explanation: 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.
7. What is size of int in C ?
A. 2 bytes
B. 4 bytes
C. 8 bytes
D. Depends on the system/compiler
View Answer
Ans : D
Explanation: The size of the datatypes depend on the system.The size of "int", in fact every other data type as well is compiler dependent and not language dependent. Based on how a compiler is implemented, it can take either 2 bytes or 4 bytes.
8. Range of double is -1.7e-38 to 1.7e+38 (in 16 bit platform - Turbo C under DOS)
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: The range of double is -1.7e+308 to 1.7e+308.
9. Which is false?
A. Constant variables need not be defined as they are declared and can be defined later
B. Global constant variables are initialized to zero
C. const keyword is used to define constant values
D. You cannot reassign a value to a constant variable
View Answer
Ans : A
Explanation: Since the constant variable has to be declared and defined at the same time, not doing it results in an error..
10. Array is ______ datatype in C Programming language.
A. Derived Data type
B. Primitive Data type
C. Custom Data type
D. None of these
View Answer
Ans : A
Explanation: Data types simply refers to the type and size of data associated with variables and functions.It is of two types :- Fundamental Data Types and Derived Data Types. Array is Derived Data type datatype in C Programming language.
11. If you pass an array as an argument to a function, what actually gets passed?
A. Address of last element of Array
B. Value of first element
C. Base address of array
D. Value of elements in array
View Answer
Ans : C
Explanation: Base address of array is passed.
12. When double is converted to float, the value is?
A. Rounded
B. Truncated
C. Depends on the standard
D. Depends on the compiler
View Answer
Ans : D
Explanation: When double is converted to float, the value will be Depends on the compiler.
13. Which of the following is not a logical operator?
A. !
B. &&
C. ||
D. |
View Answer
Ans : D
Explanation: && - Logical AND ! - Logical NOT || - Logical OR | - Bitwise OR(used in bitwise manipulations)
14. Which of the following can have different meaning in different contexts?
A. &
B. *
C. Both A and B
D. None of the above
View Answer
Ans : A
Explanation: & have different meaning in different contexts.
15. Which of the following is not a valid declaration in C?
1. short int x;Â Â Â Â
2. signed short x;
3. short x;
4. unsigned short x;
A. 1 and 2
B. 2 and 4
C. 3 and 4
D. All are valid
View Answer
Ans : D
Explanation: All are valid. First 3 mean the same thing. 4th means unsigned.
16. The minimum number of temporary variable needed to swap the content two variables is?
A. 2
B. 3
C. 0
D. 1
View Answer
Ans : C
Explanation: Without any temporary variable ,one can swap two variables easily. For Example :- var a ,b;
a=a+b;
b=a-b;
a=a-b;
17. 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 datatype
D. All of the mentioned
View Answer
Ans : C
Explanation: short is the qualifier and int is the basic datatype.
18. The precedence of arithmetic operators is (from highest to lowest)?
A. %, *, /, +, -
B. %, +, /, *, -
C. %, +, -, *, /
D. +, -, %, *, /
View Answer
Ans : A
Explanation: All arithmetic operators in C language follow the left to right associativity. Their precedence from highest to lowest is as given below:
() => Brackets
% => Modulus
* => Multiplication
/ => Division
+ => Addition
- => Subtraction
In an arithmetic operation, the higher precedence operators are evaluated first followed by the lower value operators.
19. Which of the following data type will throw an error on modulus operation(%)?
A. int
B. char
C. float
D. long
View Answer
Ans : C
Explanation: Float data type will throw an error on modulus operation(%).
20. Relational operators cannot be used on:
A. String
B. float
C. long
D. structure
View Answer
Ans : D
Explanation: structure cannot be used on Relational operators.
21. What is the output of this program?
int main()
{
char ch;
ch = 128;
printf("%d", ch);
return 0;
}
A. 128
B. -128
C. Depends on compiler
D. None of the above
View Answer
Ans : B
Explanation: signed char will be a negative number.
22. What is the output of this program?
int main()
{
float x = 'a';
printf("%f", x);
return 0;
}
A. a
B. 0.000000
C. 97.000000
D. Run time error
View Answer
Ans : C
Explanation: ASCII value of a is assigned to the float variable x .
23. How would you round off a value from 6.66 to 7.0?
A. ceil(6.66)
B. floor(6.66)
C. roundup(6.66)
D. roundto(6.66)
View Answer
Ans : A
Explanation: The ceil function returns the smallest integer that is greater than or equal to x.
24. What will be the output of the program in 16 bit platform ?
#include <stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d", sizeof(i));
return 0;
}
A. 2
B. 4
C. 8
D. Linker Error
View Answer
Ans : D
Explanation: The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
25. What will be output when you will execute following c code?
#include <stdio.h>
int main()
{
printf("%d ",sizeof(5.5));
printf("%d ",sizeof(50000));
printf("%d",sizeof('A'));
return 0;
}
A. 4 2 1
B. 8 4 4
C. 8 4 2
D. compiler dependent
View Answer
Ans : D
Explanation: None
26. What is the output of this program?
void main()
{
int i=0, j=1, k=2, m;
m = i++ || j++ || k++;
printf("%d %d %d %d", m, i, j, k);
}
A. 1 1 2 3
B. 1 1 2 2
C. 0 1 2 2
D. 0 1 2 3
View Answer
Ans : B
Explanation: n an expression involving || operator, evaluation takes place from left to right and will be stopped if one of its components evaluates to true(a non zero value).
So in the given expression m = i++ || j++ || k++.
It will be stop at j and assign the current value of j in m.
therefore m = 1 , i = 1, j = 2 and k = 2 (since k++ will not encounter. so its value remain 2)
27. What is the output of this program?
void main()
{
int c = - -14;
printf("%d", c);
}
A. 13
B. 14
C. -14
D. Compilation Error
View Answer
Ans : B
Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus = plus.
28. What is the output of this program?
void main()
{
int i=5;
i = !i>10;
printf("%d", i);
}
A. 5
B. 10
C. 0
D. None of the above
View Answer
29. What is the output of this program?
void main()
{
int p, q, r, s;
p = 1;
q = 2;
r = p, q;
s = (p, q);
printf("p=%d q=%d", p, q);
}
A. p=1 q=1
B. p=1 q=2
C. p=2 q=2
D. Invalid Syntex
View Answer
Ans : B
Explanation: The comma operator evaluates both of its operands and produces the value of the second. It also has lower precedence than assignment. Hence r = p, q is equivalent to r = p, while s = (p, q) is equivalent to s = q.
30. What is the output of this program?
void main()
{
printf("%x",-1<<4);
}
A. fff0
B. fff1
C. fff2
D. fff3
View Answer
Ans : A
Explanation: -1 will be represented in binary form as:
1111 1111 1111 1111
Now -1<<4 means 1 is Shifted towards left by 4 positions, hence it becomes:
1111 1111 1111 0000 in hexadecimal form - fff0.
31. What is the output of this program?
#include <stdio.h>
void main()
{
int a=1, b=2, c=3, d;
d = (a=c, b+=a, c=a+b+c);
printf("%d %d %d %d", d, a, b, c);
}
A. 11 3 5 11
B. 11 1 5 11
C. 11 3 2 11
D. 11 3 3 11
View Answer
Ans : A
Explanation: For any comma separated expression the outcome is the right most part.
32. What is the output of this program?
void main()
{
int a, b = 5, c;
a = 5 * (b++);
c = 5 * (++b);
printf("%d %d",a,c);
}
A. 30 35
B. 30 30
C. 25 30
D. 25 35
View Answer
Ans : D
Explanation: a = 5 * 5 and b = 5 * 7
33. What is the output of this program?
#include <stdio.h>
int main(){
printf("%d",EOF);
return 0;
}
A. 0
B. 1
C. -1
D. NULL
View Answer
Ans : C
Explanation: EOF is macro which has been defined in stdio.h and it is equivalent to -1.
34. What is the output of this program?
#include <stdio.h>
int main(){
char num = ' 10';
printf("%d", num);
return 0;
}
A. 49
B. 48
C. 10
D. 8
View Answer
Ans : B
Explanation: It will print the ascii value of 0, i.e it will print the ascii value of last character always
35. What is the output of this program?
#include <stdio.h>
int main(){
void num=10;
printf("%v", num);
return 0;
}
A. Compilation error
B. 10
C. Garbage value
D. 0
View Answer
Ans : A
Explanation: Void is not a valid data type for declaring variables.
36. What is the output of this program?
#include <stdio.h>
int main(){
printf("%d ",sizeof(2.5));
printf("%d ",sizeof(2));
printf("%d",sizeof('A'));
return 0;
}
A. 8 4 2
B. 4 4 1
C. 8 4 1
D. 4 4 2
View Answer
Ans : B
Explanation: C compiler by default will assign any undeclared float data type as double. Thus 8 4 1 is outputted
37. What is the output of this program?
#include <stdio.h>
int main(){
signed a;
unsigned b;
a = 6u + -16 + 16u + -6;
b = a + 1;
if(a == b)
printf("%d %d",a,b);
else
printf("%u %u",a, b);
return 0;
}
A. Compilation error
B. 1 0
C. 0 0
D. 0 1
View Answer
Ans : D
Explanation: Clearly, a != b and it execute the else part, where we ask compiler to display the value of a and b.
38. By default a real number is treated as a
A. float
B. double
C. long double
D. far double
View Answer
Ans : B
Explanation: A double is a more accurate way of representing floating point numbers due to more digits of precision and defaulting to a double for constants will yield more accurate and consistent answers.
39. The format identifier %i is also used for _____ data type?
A. char
B. int
C. float
D. double
View Answer
Ans : B
Explanation: Both %d and %i can be used as a format identifier for int data type.
40. Variable names beginning with underscore is not encouraged. Why?
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
Ans : C
Explanation: To avoid conflicts since library routines use such names.
Also check :
Discussion