# Data Types In C Programming MCQ

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

a) 31
b) 63
c) 12
d) 14

C99 standard guarantees the uniqueness of 63 characters for internal names.

ISO C99 compiler may consider only the first 63 characters for internal names.

2. C99 standard guarantees uniqueness of ___________ characters for external names.

a) 31
b) 6
c) 12
d) 14

C99 standard guarantees the uniqueness of 31 characters for external names.

ISO C99 compiler may consider only the 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

All of the given options are valid for variable name declaration.

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

int 3_a; is not a valid variable name declaration since variable names cannot start with a digit.

5. Why do variable names beginning with the underscore 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

The variable names beginning with the underscore are not encouraged because it avoids conflicts since library routines use such names.

6. All keywords in C are in ____________

a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned

• Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
• Keywords are part of the syntax and they cannot be used as an identifier.
• All keywords in C are in LowerCase letters.
• In C, the tolower() function is used to convert uppercase letters to lowercase.
• When an uppercase letter is passed into the tolower() function, it converts it into lowercase.

7. Variable name resolution (number of significant characters for the uniqueness of variable) depends on ___________

c) C language
d) None of the mentioned

Variable name resolution (number of significant characters for the uniqueness of variable) depends on Compiler and linker implementations. It depends on the standard to which compiler and linkers are adhering.

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; Answer: D int$main; is not a valid C variable name, 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)
d) Variable can be of any length

According to the syntax for C variable name, it cannot start with a digit. Hence option C incorrect answer.

1. Which is a valid C expression?

a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000; Answer: B int my_num = 100000; valid C expression since space, comma, and$ cannot be used in a variable name.

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

#include
int main()
{
printf("Hello World! %d n", x);
return 0;
}

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!

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? #include int main() { int y = 10000; int y = 34; printf("Hello World! %dn", y); return 0; } a) Compile time error b) Hello World! 34 c) Hello World! 1000 d) Hello World! followed by a junk value Answer: A 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: the 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

#define PI 3.14 as a macro preprocessor, it is a textual substitution. The C preprocessor is a macro preprocessor (which allows you to define macros) that transforms your program before it is compiled.

5. What will happen if the following C code is executed?

#include
int main()
{
int main = 3;
printf("%d", main);
return 0;
}

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

A C program can have the same function name and same variable name.

$cc pgm3.c$ a.out

6. What is the problem with the following variable declaration?

float 3Bedroom-Hall-Kitchen?;

a) The variable name begins with an integer
b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned

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?

#include
int main()
{
int ThisIsVariableName = 12;
int ThisIsVariablename = 14;
printf("%d", ThisIsVariablename);
return 0;
}

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

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

• A variable name can start with the alphabet, and underscore only.
• Keywords are words that have special meaning to the C compiler.
• volatile cannot be a variable name in C since volatile is a C keyword.

2. The format identifier ‘%i’ is also used for the ________ data type.

a) char
b) int
c) float
d) double

The format identifier ‘%i’ is also used for the int data type. Both %d and %i can be used as a format identifier for the 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

The unsigned short data type is most suitable for storing a number 65000 in a 32-bit system.

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 shorts.

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

The UDT (User-Defined Data Type) is a typical data type that we can derive out of any existing data type in a program. 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

An int variable is used to store an integer.

The size of a signed int or unsigned int item is the standard size of an integer on a particular machine. , The size of the data types depends on the system.

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

In C, the short int data type occupies 2 bytes (16 bits) of memory to store an integer value. Short is the qualifier and int is the basic data type in C.

A short integer can represent a whole number that may take less storage, while having a smaller range, compared with a standard integer on the same machine.

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

char has less bytes than int and int has less bytes than double in any system.

char < int < double is correct with respect to the size of the data types.

7. Which of the data types has a size that is variable?

a) int
b) struct
c) float
d) double

• struct data types have a size that is variable since the size of the structure depends on its fields, it has a variable size.
• A struct data type represents a collection of elements of different data types.
• A struct data type has an associated schema that defines the structure of the data.

3. In the following code snippet, character pointer str holds a reference to the string ___________

char *str = ".com " "training classes";

a) .com
b) .com training classes
c) .comtraining classes
d) Invalid declaration

The character pointer str holds a reference to the string .com training classes.

‘ ’ is accepted as a char in the string. Even though strlen will give the length of string “.com”, in memory str is pointing to the entire string including training classes.

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

#include
#define a 10
int main()
{
const int a = 5;
printf("a = %dn", a);
}

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error

The #define substitutes a with 10 without leaving any identifier, which results in a 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? #include int main() { int var = 010; printf("%d", var); } a) 2 b) 8 c) 9 d) 10 Answer: B 010 is an octal representation of 8. Output:$ cc pgm4.c
$a.out 6. What will be the output of the following C function? #include enum birds {SPARROW, PEACOCK, PARROT}; enum animals {TIGER = 8, LION, RABBIT, ZEBRA}; int main() { enum birds m = TIGER; int k; k = m; printf("%dn", k); return 0; } a) 0 b) Compile time error c) 1 d) 8 Answer: D m is an integer constant, hence it is compatible. Output:$ cc pgm5.c
$a.out 7. What will be the output of the following C code? #include #define MAX 2 enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX}; int main() { enum bird b = PARROT; printf("%dn", b); return 0; } a) Compilation error b) 5 c) Undefined value d) 2 Answer: B 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? #include #include int main() { char *str = "x"; char c = 'x'; char ary[1]; ary[0] = c; printf("%d %d", strlen(str), strlen(ary)); return 0; } a) 1 1 b) 2 1 c) 2 2 d) 1 (undefined value) Answer: D str is null-terminated, but ary is not null-terminated. Output:$ cc pgm7.c
$a.out 1 5 1. Enum types are processed by _________ a) Compiler b) Preprocessor c) Linker d) Assembler Answer: A • An enum type is a special data type that enables for a variable to be a set of predefined constants. • The variable must be equal to one of the values that have been predefined for it. • Enum types are processed by Compiler. 4. What will be the output of the following C code? #include int main() { const int p; p = 4; printf("p is %d", p); return 0; } a) p is 4 b) Compile time error c) Run time error d) p is followed by a garbage value Answer: B 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’

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

#include
void main()
{
int k = 4;
int *const p = &k;
int r = 3;
p = &r;
printf("%d", p);
}

c) Compile time error

Since the pointer p is declared to be constant, trying to assign it with a new value results in an error.

Output:

$cc pgm11.c pgm11.c: In function ‘main’: pgm11.c:7: error: assignment of read-only variable ‘p’ pgm11.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’ 6. Which of the following statement is false about constant variables? 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 Answer: A The constant variable has to be declared and defined at the same time, not doing it results in an error. A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration. 7. What will be the output of the following C code? #include void main() { int const k = 5; k++; printf("k is %d", k); } a) k is 6 b) Error due to const succeeding int c) Error because a constant variable can be changed only twice d) Error, because a constant variable cannot be changed Answer: D The constant variable has to be declared and defined at the same time. Trying to change it results in an error. Output:$ cc pgm12.c
pgm12.c: In function ‘main’:
pgm12.c:5: error: increment of read-only variable ‘k’.

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

#include
int const print()
{
printf(".com");
return 0;
}
void main()
{
print();
}

a) Error because function name cannot be preceded by const
b) .com
c) .com is printed infinite times
d) Blank screen, no output

Output:

$cc pgm13.c$ a.out
.com

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

#include
void foo(const int *);
int main()
{
const int i = 10;
printf("%d ", i);
foo(&i);
printf("%d", i);

}
void foo(const int *i)
{
*i = 20;
}

a) Compile time error
b) 10 20
c) Undefined value
d) 10

Cannot change a const type value.

Output:

$cc pgm1.c pgm1.c: In function ‘foo’: pgm1.c:13: error: assignment of read-only location ‘*i’ 2. What will be the output of the following C code? #include int main() { const int i = 10; int *ptr = &i; *ptr = 20; printf("%dn", i); return 0; } a) Compile time error b) Compile time warning and printf displays 20 c) Undefined behavior d) 10 Answer: B Changing const variables through non-constant pointers invokes a compiler warning. Output:$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$a.out 20 3. What will be the output of the following C code? #include int main() { j = 10; printf("%dn", j++); return 0; } a) 10 b) 11 c) Compile time error d) 0 Answer: C Variable j is not defined. Output:$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:4: error: ‘j’ undeclared (first use in this function)
pgm3.c:4: error: (Each undeclared identifier is reported only once
pgm3.c:4: error: for each function it appears in.)

4. Will the following C code compile without any error?

#include
int main()
{
for (int k = 0; k < 10; k++);
return 0;
}

a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) Error

Compilers implementing C90 do not allow this, but compilers implementing C99 allow it.

Output:

$cc pgm4.c pgm4.c: In function ‘main’: pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code. 5. Will the following C code compile without any error? #include int main() { int k; { int k; for (k = 0; k < 10; k++); } } a) Yes b) No c) Depends on the compiler d) Depends on the C standard implemented by compilers Answer: A There can be blocks inside the block. But within a block, variables have only block scope. Output:$ cc pgm5.c

6. Which of the following declaration is not supported by C?

a) String str;
b) char *str;
c) float str = 3e2;
d) Both String str; & float str = 3e2;

String str; is declaration is not supported by C.  It is legal in Java, but not in C. ‘C’ language does not directly support string as a data type. Hence, to display a String in C, you need to make use of a character array.

7. Which of the following format identifier can never be used for the variable var?

#include
int main()
{
char *var = "Advanced Training in C by .com";
}

a) %f
b) %d
c) %c
d) %s

%c can be used to print the indexed position.
%d can still be used to display its ASCII value.
%s is recommended.
%f cannot be used for the variable var.

1. Which of the following declaration is illegal?

a) char *str = “Best C programming classes by ”;
b) char str[] = “Best C programming classes by ”;
c) char str[20] = “Best C programming classes by ”;
d) char[] str = “Best C programming classes by ”;

‘C’ language does not directly support string as a data type. Hence, to display a String in C, you need to make use of a character array.
char[] str is a declaration in Java, but not in C.

2. Which keyword is used to prevent any changes in the variable within a C program?

a) immutable
b) mutable
c) const
d) volatile

The const keyword is used to prevent any changes in the variable within a C program. const is a keyword constant in the C program.

3. Which of the following is not a pointer declaration?

a) char a[10];
b) char a[] = {‘1’, ‘2’, ‘3’, ‘4’};
c) char *str;
d) char a;

char a; is not a pointer declaration Array declarations are pointer declarations.

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

#include
void main()
{
int k = 4;
float k = 4;
printf("%d", k)
}

a) Compile time error
b) 4
c) 4.0000000
d) 4.4

Since the variable k is defined both as an integer and as float, it results in an error.

Output:

$cc pgm8.c pgm8.c: In function ‘main’: pgm8.c:5: error: conflicting types for ‘k’ pgm8.c:4: note: the previous definition of ‘k’ was here pgm8.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’ pgm8.c:7: error: expected ‘;’ before ‘}’ token 5. Which of the following statement is false about the variable in C? a) A variable defined once can be defined again with different scope b) A single variable cannot be defined with two different types in the same scope c) A variable must be declared and defined at the same time d) A variable refers to a location in memory Answer: C It is not an error if the variable is declared and not defined. For example – extern declarations. 6. A variable declared in a function can be used in main(). a) True b) False c) True if it is declared static d) None of the mentioned Answer: B Since the scope of the variable declared within a function is restricted only within that function, so the above statement is false. 7. The name of the variable used in one function cannot be used in another function. a) True b) False Answer: B Since the scope of the variable declared within a function is restricted only within that function, the same name can be used to declare another variable in another function. 1. What will be the output of the following C code? #include int main() { int i = -3; int k = i % 2; printf("%dn", k); } a) Compile time error b) -1 c) 1 d) Implementation defined Answer: B 2. What will be the output of the following C code? #include int main() { int i = 3; int l = i / -2; int k = i % -2; printf("%d %dn", l, k); return 0; } a) Compile time error b) -1 1 c) 1 -1 d) Implementation defined Answer: B 3. What will be the output of the following C code? #include int main() { int i = 5; i = i / 3; printf("%dn", i); return 0; } a) Implementation defined b) 1 c) 3 d) Compile time error Answer: B 4. What will be the output of the following C code? #include int main() { int i = -5; i = i / 3; printf("%dn", i); return 0; } a) Implementation defined b) -1 c) -3 d) Compile time error Answer: B 5. What will be the final value of x in the following C code? #include void main() { int x = 5 * 9 / 3 + 9; } a) 3.75 b) Depends on the compiler c) 24 d) 3 Answer: C 6. What will be the output of the following C code? #include void main() { int x = 5.3 % 2; printf("Value of x is %d", x); } a) Value of x is 2.3 b) Value of x is 1 c) Value of x is 0.3 d) Compile time error Answer: D 7. What will be the output of the following C code? #include void main() { int y = 3; int x = 5 % 2 * 3 / 2; printf("Value of x is %d", x); } a) Value of x is 1 b) Value of x is 2 c) Value of x is 3 d) Compile time error Answer: A 1. What will be the output of the following C code? #include void main() { int a = 3; int b = ++a + a++ + --a; printf("Value of b is %d", b); } a) Value of x is 12 b) Value of x is 13 c) Value of x is 10 d) Undefined behavior Answer: D 2. What is the precedence of arithmetic operators (from highest to lowest)? a) %, *, /, +, – b) %, +, /, *, – c) +, -, %, *, / d) %, +, -, *, / Answer: A The precedence of arithmetic operators (from highest to lowest) is %, *, /, +, –. An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division, etc on numerical values (constants and variables). 3. Which of the following is not an arithmetic operation? a) a * = 10; b) a / = 10; c) a ! = 10; d) a % = 10; Answer: C An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division, etc on numerical values (constants and variables). a ! = 10; is not an arithmetic operation. 4. Which of the following data type will throw an error on modulus operation(%)? a) char b) short c) int d) float Answer: D The float data type will throw an error on modulus operation(%). The FLOAT data type stores double-precision floating-point numbers with up to 17 significant digits. 5. Which among the following are the fundamental arithmetic operators, i.e, performing the desired operation can be done using that operator only? a) +, – b) +, -, % c) +, -, *, / d) +, -, *, /, % Answer: A An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division, etc on numerical values (constants and variables). +, – are the fundamental arithmetic operators. 6. What will be the output of the following C code? #include int main() { int a = 10; double b = 5.6; int c; c = a + b; printf("%d", c); } a) 15 b) 16 c) 15.6 d) 10 Answer: A 7. What will be the output of the following C code? #include int main() { int a = 10, b = 5, c = 5; int d; d = a == (b + c); printf("%d", d); } a) Syntax error b) 1 c) 10 d) 5 Answer: B 1. What will be the output of the following C code? #include void main() { int x = 1, y = 0, z = 5; int a = x && y || z++; printf("%d", z); } a) 6 b) 5 c) 0 d) Varies Answer: A 2. What will be the output of the following C code? #include void main() { int x = 1, y = 0, z = 5; int a = x && y && z++; printf("%d", z); } a) 6 b) 5 c) 0 d) Varies Answer: B 3. What will be the output of the following C code? #include int main() { int x = 1, y = 0, z = 3; x > y ? printf("%d", z) : return z; } a) 3 b) 1 c) Compile time error d) Run time error Answer: C 4. What will be the output of the following C code? #include void main() { int x = 1, z = 3; int y = x << 3; printf(" %dn", y); } a) -2147483648 b) -1 c) Run time error d) 8 Answer: D 5. What will be the output of the following C code? #include void main() { int x = 0, y = 2, z = 3; int a = x & y | z; printf("%d", a); } a) 3 b) 0 c) 2 d) Run time error Answer: A 6. What will be the final value of j in the following C code? #include int main() { int i = 0, j = 0; if (i && (j = i + 10)) //do something ; } a) 0 b) 10 c) Depends on the compiler d) Depends on language standard Answer: A 7. What will be the final value of j in the following C code? #include int main() { int i = 10, j = 0; if (i || (j = i + 10)) //do something ; } a) 0 b) 20 c) Compile time error d) Depends on language standard Answer: A 8. What will be the output of the following C code? #include int main() { int i = 1; if (i++ && (i == 1)) printf("Yesn"); else printf("Non"); } a) Yes b) No c) Depends on the compiler d) Depends on the standard Answer: B 1. Are logical operator sequence points? a) True b) False c) Depends on the compiler d) Depends on the standard Answer: A Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to not, followed by and, and finally by or. Logical Operators are not sequenced points. 2. Do logical operators in the C language evaluated with the short circuit? a) True b) False c) Depends on the compiler d) Depends on the standard Answer: A It is true that logical operators in the C language are evaluated with the short circuit. Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to not, followed by and, and finally by or. 3. What is the result of the logical or relational expression in C? a) True or False b) 0 or 1 c) 0 if an expression is false and any positive number if an expression is true d) None of the mentioned Answer: B • An expression is a combination of variable constants and operators written according to the syntax of the C language. • The result of the logical or relational expression in C is 0 if an expression is false and any positive number if an expression is true. 4. What will be the final value of d in the following C code? #include int main() { int a = 10, b = 5, c = 5; int d; d = b + c == a; printf("%d", d); } a) Syntax error b) 1 c) 5 d) 10 Answer: B 5. What will be the output of the following C code? #include int main() { int a = 10, b = 5, c = 3; b != !a; c = !!a; printf("%dt%d", b, c); } a) 5 1 b) 0 3 c) 5 3 d) 1 1 Answer: A 6. Which among the following is NOT a logical or relational operator? a) != b) == c) || d) = Answer: D 7. What will be the output of the following C code? #include int main() { int a = 10; if (a == a--) printf("TRUE 1t"); a = 10; if (a == --a) printf("TRUE 2t"); } a) TRUE 1 b) TRUE 2 c) TRUE 1 TRUE 2 d) Compiler Dependent Answer: D This is a sequence point problem and hence the result will be implementation dependent. 8. Relational operators cannot be used on ____________ a) structure b) long c) strings d) float Answer: A • A relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. • Relational operators cannot be used on the structure. 1. What will be the output of the following C code? #include void main() { float x = 0.1; if (x == 0.1) printf(""); else printf("Advanced C Classes"); } a) Advanced C Classes b) c) Run time error d) Compile time error Answer: A 2. What will be the output of the following C code? #include void main() { float x = 0.1; printf("%d, ", x); printf("%f", x); } a) 0.100000, junk value b) Junk value, 0.100000 c) 0, 0.100000 d) 0, 0.999999 Answer: B 3. What will be the output of the following C code? (Initial values: x= 7, y = 8) #include void main() { float x; int y; printf("enter two numbers n", x); scanf("%f %f", &x, &y); printf("%f, %d", x, y); } a) 7.000000, 7 b) Run time error c) 7.000000, junk d) Varies Answer: C 4. What will be the output of the following C code? #include void main() { double x = 123828749.66; int y = x; printf("%dn", y); printf("%lfn", y); } a) 0, 0.0 b) 123828749, 123828749.66 c) 12382874, 12382874.0 d) 123828749, 0.000000 Answer: D 5. What will be the output of the following C code? #include void main() { int x = 97; char y = x; printf("%cn", y); } a) a b) b c) 97 d) Run time error Answer: A 6. When double is converted to float, then the value is? a) Truncated b) Rounded c) Depends on the compiler d) Depends on the standard Answer: C When double is converted to float, then the value depends on the compiler. Real numbers are represented in C by the floating point types float, double, and long double. 7. What will be the output of the following C code? #include int main() { unsigned int i = 23; signed char c = -23; if (i > c) printf("Yesn"); else if (i < c) printf("Non"); } a) Yes b) No c) Depends on the compiler d) Depends on the operating system Answer: B 8. What will be the output of the following C code? #include int main() { int i = 23; char c = -23; if (i < c) printf("Yesn"); else printf("Non"); } a) Yes b) No c) Depends on the compiler d) Depends on the standard Answer: B 1. function tolower(c) defined in library works for ___________ a) Ascii character set b) Unicode character set c) Ascii and utf-8 but not EBCDIC character set d) Any character set Answer: D • The function tolower(c) defined in the library works for any character set. • The tolower() function takes an uppercase alphabet and converts it to a lowercase character. 2. What will be the output of the following C code considering the size of a short int is 2, char is 1 and int is 4 bytes? #include int main() { short int i = 20; char c = 97; printf("%d, %d, %dn", sizeof(i), sizeof(c), sizeof(c + i)); return 0; } a) 2, 1, 2 b) 2, 1, 1 c) 2, 1, 4 d) 2, 2, 8 Answer: C 3. Which type of conversion is NOT accepted? a) From char to int b) From float to char pointer c) From negative int to char d) From double to char Answer: B Conversion of a float to pointer type is not allowed. The data type of the pointer and the variable must match, an int pointer can hold the address of an int variable similarly, and a pointer declared with a float data type can hold the address of a float variable. 4. What will be the data type of the result of the following operation? (float)a * (int)b / (long)c * (double)d a) int b) long c) float d) double Answer: D The result of the following operation will be doubled. 5. Which of the following type-casting has chances for wrap-around? a) From int to float b) From int to char c) From char to short d) From char to int Answer: B From int to char type-casting has chances for wrap-around. We can convert an integer to a character by adding a ‘0’ (zero) character. The char data type is represented as ASCII values in c programming. 6. Which of the following types of casting is accepted by C? a) Widening conversions b) Narrowing conversions c) Widening & Narrowing conversions d) None of the mentioned Answer: C • Widening & Narrowing conversions types of casting are accepted by C. • Widening conversions preserve the source value but can change its representation. • A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. 7. When do you need to use type-conversions? a) The value to be stored is beyond the max limit b) The value to be stored is in a form not supported by that data type c) To reduce the memory in use, relevant to the value d) All of the mentioned Answer: D In C programming, we can convert the value of one data type ( int, float, double, etc.) to another. This process is known as type conversion. The need for type conversion is due to • The value to be stored is beyond the max limit • The value to be stored is in a form not supported by that data type • To reduce the memory in use, relevant to the value 2. What will be the output of the following C code? #include int main() { int i = 10; int *p = &i; printf("%dn", *p++); } a) 10 b) 11 c) Garbage value d) Address of i Answer: A 3. What will be the output of the following C code? #include void main() { int x = 97; int y = sizeof(x++); printf("X is %d", x); } a) X is 97 b) X is 98 c) X is 99 d) Run time error Answer: A 4. What will be the output of the following C code? #include void main() { int x = 4, y, z; y = --x; z = x--; printf("%d%d%d", x, y, z); } a) 3 2 3 b) 2 3 3 c) 3 2 2 d) 2 3 4 Answer: B 5. What will be the output of the following C code? #include void main() { int x = 4; int *p = &x; int *k = p++; int r = p - k; printf("%d", r); } a) 4 b) 8 c) 1 d) Run time error Answer: C 6. What will be the output of the following C code? #include void main() { int a = 5, b = -7, c = 0, d; d = ++a && ++b || ++c; printf("n%d%d%d%d", a, b, c, d); } a) 6 -6 0 0 b) 6 -5 0 1 c) -6 -6 0 1 d) 6 -6 0 1 Answer: D 7. What will be the output of the following C code? #include void main() { int a = -5; int k = (a++, ++a); printf("%dn", k); } a) -4 b) -5 c) 4 d) -3 Answer: D 2. What will be the output of the following C code? #include int main() { int a = 1, b = 1, c; c = a++ + b; printf("%d, %d", a, b); } a) a = 1, b = 1 b) a = 2, b = 1 c) a = 1, b = 2 d) a = 2, b = 2 Answer: B 3. What will be the output of the following C code? #include int main() { int a = 1, b = 1, d = 1; printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++ + a++); } a) 15, 4, 5 b) 9, 6, 9 c) 9, 3, 5 d) Undefined (Compiler Dependent) Answer: D 4. For which of the following does the “PI++;” code will fail? a) #define PI 3.14 b) char *PI = “A”; c) float PI = 3.14; d) none of the Mentioned Answer: A For #define PI 3.14 “PI++;” code will fail. In C, ++ and — operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as — operator can appear before or after the operand with the same effect. That means both i++ and ++i will be equivalent. 5. What will be the output of the following C code? #include int main() { int a = 10, b = 10; if (a = 5) b--; printf("%d, %d", a, b--); } a) a = 10, b = 9 b) a = 10, b = 8 c) a = 5, b = 9 d) a = 5, b = 8 Answer: C 6. What will be the output of the following C code? #include int main() { int i = 0; int j = i++ + i; printf("%dn", j); } a) 0 b) 1 c) 2 d) Compile time error Answer: B 7. What will be the output of the following C code? #include int main() { int i = 2; int j = ++i + i; printf("%dn", j); } a) 6 b) 5 c) 4 d) Compile time error Answer: A 8. What will be the output of the following C code? #include int main() { int i = 2; int i = i++ + i; printf("%dn", i); } a) = operator is not a sequence point b) ++ operator may return value with or without side effects c) it can be evaluated as (i++)+i or i+(++i) d) = operator is a sequence point Answer: A 1. What will be the output of the following C code? #include void main() { int a = 5, b = -7, c = 0, d; d = ++a && ++b || ++c; printf("n%d%d%d%d", a, b, c, d); } a) 6 -6 0 0 b) 6 -5 0 1 c) -6 -6 0 1 d) 6 -6 0 1 Answer: D 2. What will be the output of the following C code? #include void main() { int a = -5; int k = (a++, ++a); printf("%dn", k); } a) -3 b) -5 c) 4 d) Undefined Answer: A 3. What will be the output of the following C code? #include int main() { int x = 2; x = x << 1; printf("%dn", x); } a) 4 b) 1 c) Depends on the compiler d) Depends on the endianness of the machine Answer: A 4. What will be the output of the following C code? #include int main() { int x = -2; x = x >> 1; printf("%dn", x); } a) 1 b) -1 c) 2 31 – 1 considering int to be 4 bytes d) Either -1 or 1 Answer: B 5. What will be the output of the following C code? #include int main() { if (~0 == 1) printf("yesn"); else printf("non"); } a) yes b) no c) compile time error d) undefined Answer: B 6. What will be the output of the following C code? #include int main() { int x = -2; if (!0 == 1) printf("yesn"); else printf("non"); } a) yes b) no c) run time error d) undefined Answer: A 7. What will be the output of the following C code? #include int main() { int y = 0; if (1 |(y = 1)) printf("y is %dn", y); else printf("%dn", y); } a) y is 1 b) 1 c) run time error d) undefined Answer: A 8. What will be the output of the following C code? #include int main() { int y = 1; if (y & (y = 2)) printf("true %dn", y); else printf("false %dn", y); } a) true 2 b) false 2 c) either true 2 or false 2 d) true 1 Answer: A 1. Which of these is not a bitwise operator? a) & b) &= c) |= d) <= Answer: D The Bitwise Operator in C is a type of operator that operates on bit arrays, bit strings, and tweaking binary values with individual bits at the bit level. <= is a relational operator. 2. Which operator is used to invert all the digits in a binary representation of a number? a) ~ b) <<< c) >>> d) ^ Answer: A An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc. ~operator is used to inverting all of the bits of its operand in binary representation. 3. On applying the Left shift operator, <<, on integer bits are lost once they are shifted past which position bit? a) 1 b) 32 c) 33 d) 31 Answer: D The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, and zero is brought in from the right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31. 4. Which right shift operator preserves the sign of the value? a) << b) >> c) <<= d) >>= Answer: B An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise, etc. >> is a right shift operator that preserves the sign of the value. 5. Which of these statements are incorrect about the bitwise operators? a) The left shift operator, <<, shifts all of the bits in a value to the left specified number of times b) The right shift operator, >>, shifts all of the bits in a value to the right specified number of times c) The left shift operator can be used as an alternative to multiplying by 2 d) The right shift operator automatically fills the higher order bits with 0 Answer: D The right shift operator automatically fills the higher order bit with its previous contents each time a shift occurs. This also preserves the sign of the value. 6. What will be the output of the following Java program? class bitwise_operator { public static void main(String args[]) { int var1 = 42; int var2 = ~var1; System.out.print(var1 + " " + var2); } } a) 42 42 b) 43 43 c) 42 -43 d) 42 43 Answer: C Unary not operator, ~, invert all of the bits of its operand. 42 in binary is 00101010 in using the ~ operator on var1 and assigning it to var2 we get the inverted value of 42 i:e 11010101 which is -43 in decimal. output: $ javac bitwise_operator.java
$java bitwise_operator 42 -43 7. What will be the output of the following Java program? class bitwise_operator { public static void main(String args[]) { int a = 3; int b = 6; int c = a | b; int d = a & b; System.out.println(c + " " + d); } } a) 7 2 b) 7 7 c) 7 5 d) 5 2 Answer: A And operator produces 1 bit if both operands is 1. Or operator produces 1 bit if any bit of the two operands in 1. output: $ javac bitwise_operator.java
$java bitwise_operator 7 2 8. What will be the output of the following Java program? class leftshift_operator { public static void main(String args[]) { byte x = 64; int i; byte y; i = x << 2; y = (byte) (x << 2) System.out.print(i + " " + y); } } a) 0 64 b) 64 0 c) 0 256 d) 256 0 Answer: D output: $ javac leftshift_operator.java
$java leftshift_operator 256 0 9. What will be the output of the following Java program? class rightshift_operator { public static void main(String args[]) { int x; x = 10; x = x >> 1; System.out.println(x); } } a) 10 b) 5 c) 2 d) 20 Answer: B The right shift operator, >>, divides the value by 2. output: $ javac rightshift_operator.java
$java rightshift_operator 5 10. What will be the output of the following Java program? class Output { public static void main(String args[]) { int a = 1; int b = 2; int c = 3; a |= 4; b >>= 1; c <<= 1; a ^= c; System.out.println(a + " " + b + " " + c); } } a) 3 1 6 b) 2 2 3 c) 2 3 4 d) 3 3 6 Answer: A output: $ javac Output.java
\$ java Output
3 1 6

1. What is the type of the following assignment expression if x is of type float and y is of type int?

y = x + y;

a) int
b) float
c) there is no type for an assignment expression
d) double

int is a type for an assignment expression if x is of type float and y is of type int.

2. What will be the value of the following assignment expression?

(x = foo())!= 1 considering foo() returns 2

a) 2
b) True
c) 1
d) 0

For assignment expression (x = foo())!= 1 considering foo() returns 2. The value will be 2.

3. Operation “a = a * b + a” can also be written as __________

a) a *= b + 1;
b) (c = a * b)!=(a = c + a);
c) a = (b + 1)* a;
d) All of the mentioned

Operation “a = a * b + a” can also be written as

1. a *= b + 1;
2. (c = a * b)!=(a = c + a);
3. a = (b + 1)* a;

4. What will be the final value of c in the following C statement? (Initial value: c = 2)

a) c = 1;
b) c = 2;
c) c = 3;
d) c = 4;

The final value of c in the (Initial value: c = 2) statement is c = 4;

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

#include
int main()
{
int a = 1, b = 2;
a += b -= a;
printf("%d %d", a, b);
}

a) 1 1
b) 1 2
c) 2 1
d) 2 2

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

#include
int main()
{
int a = 4, n, i, result = 0;
scanf("%d", n);
for (i = 0;i < n; i++)
result += a;
}

a) Addition of a and n
b) Subtraction of a and n
c) Multiplication of a and n
d) Division of a and n

7. Which of the following is an invalid assignment operator?

a) a %= 10;
b) a /= 10;
c) a |= 10;
d) None of the mentioned

All the assignment operators in the given option are correct.

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

#include
void main()
{
int x = 0;
if (x = 0)
printf("Its zeron");
else
printf("Its not zeron");
}

a) Its not zero
b) It’s zero
c) Run time error
d) None

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

#include
void main()
{
int k = 8;
int x = 0 == 1 && k++;
printf("%d%dn", x, k);
}

a) 0 9
b) 0 8
c) 1 8
d) 1 9

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

#include
void main()
{
char a = 'a';
int x = (a % 10)++;
printf("%dn", x);
}

a) 6
b) Junk value
c) Compile time error
d) 7

4. What will be the output of the following C code snippet?

#include
void main()
{
1 < 2 ? return 1: return 2;
}

a) returns 1
b) returns 2
c) Varies
d) Compile time error

5. What will be the output of the following C code snippet?

#include
void main()
{
unsigned int x = -5;
printf("%d", x);
}

a) Run time error
b) Aries
c) -5
d) 5

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

#include
int main()
{
int x = 2, y = 1;
x *= x + y;
printf("%dn", x);
return 0;
}

a) 5
b) 6
c) Undefined behavior
d) Compile time error

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

#include
int main()
{
int x = 2, y = 2;
x /= x / y;
printf("%dn", x);
return 0;
}

a) 2
b) 1
c) 0.5
d) Undefined behavior

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

#include
int main()
{
int x = 1, y = 0;
x &&= y;
printf("%dn", x);
}

a) Compile time error
b) 1
c) 0
d) Undefined behavior

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

#include
int main()
{
int x = 2, y = 0;
int z = (y++) ? y == 1 && x : 0;
printf("%dn", z);
return 0;
}

a) 0
b) 1
c) Undefined behavior
d) Compile time error

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

#include
int main()
{
int x = 1;
int y = x == 1 ? getchar(): 2;
printf("%dn", y);
}

a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2

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

#include
int main()
{
int x = 1;
short int i = 2;
float f = 3;
if (sizeof((x == 2) ? f : i) == sizeof(float))
printf("floatn");
else if (sizeof((x == 2) ? f : i) == sizeof(short int))
printf("short intn");
}

a) float
b) short int
c) Undefined behavior
d) Compile time error

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

#include
int main()
{
int a = 2;
int b = 0;
int y = (b == 0) ? a :(a > b) ? (b = 1): a;
printf("%dn", y);
}

a) Compile time error
b) 1
c) 2
d) Undefined behavior

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

#include
int main()
{
int y = 1, x = 0;
int l = (y++, x++) ? y : x;
printf("%dn", l);
}

a) 1
b) 2
c) Compile time error
d) Undefined behavior

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

#include
void main()
{
int k = 8;
int m = 7;
int z = k < m ? k++ : m++;
printf("%d", z);
}

a) 7
b) 8
c) Run time error
d) 15

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

#include
void main()
{
int k = 8;
int m = 7;
int z = k < m ? k = m : m++;
printf("%d", z);
}

a) Run time error
b) 7
c) 8
d) Depends on the compiler

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

#include
void main()
{
1 < 2 ? return 1 : return 2;
}

a) returns 1
b) returns 2
c) Varies
d) Compile time error

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

#include
void main()
{
int k = 8;
int m = 7;
k < m ? k++ : m = k;
printf("%d", k);
}

a) 7
b) 8
c) Compile time error
d) Run time error

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

#include
void main()
{
int k = 8;
int m = 7;
k < m ? k = k + 1 : m = m + 1;
printf("%d", k);
}

a) Compile time error
b) 9
c) 8
d) Run time error

3. What will be the final values of a and c in the following C statement? (Initial values: a = 2, c = 1)

a) a = 0, c = 0;
b) a = 2, c = 2;
c) a = 2, c = 2;
d) a = 1, c = 2;

The final values of a and c in the statement Initial values: a = 2, c = 1) is

a = 0, c = 0;

4. What will be the data type of the following expression? (Initial data type: a = int, var1 = double, var2 = float)

expression (a < 50)? var1 : var2;

a) int
b) float
c) double
d) Cannot be determined

The data type of the following expression (a < 50)? var1 : var2; is double.

5. Which expression has to be present in the following?

exp1 ? exp2 : exp3;

a) exp1
b) exp2
c) exp3
d) all of the mentioned

For expression exp1? exp2: exp3;  the following variable has to be present

1. exp1
2. exp2
3. exp3

6. What will be the final value of c in the following C code snippet? (Initial values: a = 1, b = 2, c = 1)

a) Syntax Error
b) c = 1
c) c = 2
d) c = 3

The final value of c in the C code snippet of Initial values: a = 1, b = 2, c = 1 is 3.

1. Which of the following are unary operators?

a) sizeof
b) –
c) ++
d) all of the mentioned

Unary operators are operators that act upon a single operand to produce a new value.

Types of unary operators:

1. unary minus(-)
2. increment(++)
3. decrement(- -)
4. NOT(!)
6. sizeof()

2. Where in C the order of precedence of operators does not exist?

a) Within conditional statements, if, else
b) Within a while, do-while
c) Within a macro definition
d) None of the mentioned

The precedence of operators determines which operator is executed first if there is more than one operator in an expression.

All the given options are correct hence option D is correct.

3. Associativity of an operator is ___________

a) Right to Left
b) Left to Right
c) Random fashion
d) Both Right to Left and Left to Right

Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.

4. Which of the following method is accepted for an assignment?

a) 5 = a = b = c = d;
b) a = b = c = d = 5;
c) a = b = 5 = c = d;
d) None of the mentioned

The assignment statement is used to assign a value (computed from an expression) to a variable.

a = b = c = d = 5; is accepted for assignment.

5. Which of the following is NOT possible with any 2 operators in C?

a) Different precedence, same associativity
b) Different precedence, different associativity
c) Same precedence, different associativity
d) All of the mentioned

The same precedence, different associativity is NOT possible with any 2 operators in C.

Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.

6. Which of the following is possible with any 2 operators in C?

a) Same associativity, different precedence
b) Same associativity, same precedence
c) Different associativity, different precedence
d) All of the mentioned

The following is possible with any 2 operators in C are

1. Same associativity, different precedence
2. Same associativity, same precedence
3. Different associativity, different precedence

Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.

7. Which of the following operators has the lowest precedence?

a) !=
b) &&
c) ?:
d) ,

, operators has the lowest precedence. Comma has the least precedence among all operators and should be used carefully For example consider the following program, the output is 1.

1. Which of the following operators has associativity from Right to Left?

a) <=
b) <<
c) ==
d) +=

Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.

+= operators have associativity from Right to Left.

2. Which operators of the following have the same precedence?

P. "!=", Q. "+=", R. "<<="

a) P and Q
b) Q and R
c) P and R
d) P, Q and R

Q and R operators have the same precedence.

Precedence, in C#, is the rule that specifies the order in which certain operations need to be performed in an expression.

For a given expression containing more than two operators, it determines which operations should be calculated first.

4. Which of the following option is the correct representation of the following C statement?

a) e = (a * (b +(c /(d * f))));
b) e = ((a * b) + (c / (d * f)));
c) e = ((a * b) + ((c / d)* f));
d) Both e = ((a * b) + (c / (d * f))); and e = ((a * b) + ((c / d)* f));

Both e = ((a * b) + (c / (d * f))); and e = ((a * b) + ((c / d)* f)); is the correct representation of the following C statement

Verified by e = 1 * 2 + 3 / 4 * 5; and then using respective braces according to the option.

5. While swapping 2 numbers what precautions are to be taken care?

b = (b / a);
a = a * b;
b = a / b;

a) Data type should be either of short, int and long
b) Data type should be either of float and double
c) All data types are accepted except for (char *)
d) This code doesn’t swap 2 numbers

While swapping 2 numbers Data type should be either of float and double.

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

#include
int main()
{
int a = 1, b = 2, c = 3, d = 4, e;
e = c + d = b * a;
printf("%d, %dn", e, d);
}

a) 7, 4
b) 7, 2
c) 5, 2
d) Syntax error

7. Which of the following is the correct order of evaluation for the given expression?

a) % / * =
b) / * % =
c) = % * /
d) * % / =

The correct order of evaluation for the given expression is % / * =.

8. Which function in the following expression will be called first?

a = func3(6) - func2(4, 5) / func1(1, 2, 3);

a) func1();
b) func2();
c) func3();
d) Cannot be predicted

From the expression  a = func3(6) – func2(4, 5) / func1(1, 2, 3); it is difficult to decide which expression will call first.

9. Which of the following operator has the highest precedence in the following?

a) ()
b) sizeof
c) *
d) +

() perator has the highest precedence. When two operators share a single operand, the operator having the highest precedence goes first.

10. Which of the following is a ternary operator?

a) &&
b) >>=
c) ?:
d) ->

The programmers utilize the ternary operator in case of decision-making when longer conditional statements like if and else exist.

?: is a ternary operator.

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

#include
void main()
{
int b = 5 - 4 + 2 * 5;
printf("%d", b);
}

a) 25
b) -5
c) 11
d) 16

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

#include
void main()
{
int b = 5 & 4 & 6;
printf("%d", b);
}

a) 5
b) 6
c) 3
d) 4

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

#include
void main()
{
int b = 5 & 4 | 6;
printf("%d", b);
}

a) 6
b) 4
c) 1
d) 0

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

#include
void main()
{
int b = 5 + 7 * 4 - 9 * (3, 2);
printf("%d", b);
}

a) 6
b) 15
c) 13
d) 21

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

#include
void main()
{
int h = 8;
int b = (h++, h++);
printf("%d%dn", b, h);
}

a) 10 10
b) 10 9
c) 9 10
d) 8 10

h({});

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

#include
void main()
{
int h = 8;
int b = h++ + h++ + h++;
printf("%dn", h);
}

a) 9
b) 10
c) 12
d) 11