DATA TYPES
1. Comment on the output of this C code?
#include <stdio.h>
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
a) The compiler will flag an error
b) Program will compile and print the output 5
c) Program will compile and print the ASCII value of 5
d) Program will compile and print FAIL for 5 times
View Answer
b) Program will compile and print the output 5
c) Program will compile and print the ASCII value of 5
d) 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
FAILED
FAILED
FAILED
FAILED
FAILED
Explanation:The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAILED
FAILED
FAILED
FAILED
FAILED
2. The format identifier ‘%i’ is also used for _____ data type?
a) char
b) int
c) float
d) double
View Answer
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.
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
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.
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
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.
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
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.
Explanation:The size of the data types depend on the system.
6. What is the output of this C code?
#include <stdio.h>
int main()
{
signed char chr;
chr = 128;
printf("%d\n", chr);
return 0;
}
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
View Answer
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
Explanation:signed char will be a negative number.
Output:
$ cc pgm2.c
$ a.out
-128
7. Comment on the output of this C code?
#include <stdio.h>
int main()
{
char c;
int i = 0;
FILE *file;
file = fopen("test.txt", "w+");
fprintf(file, "%c", 'a');
fprintf(file, "%c", -1);
fprintf(file, "%c", 'b');
fclose(file);
file = fopen("test.txt", "r");
while ((c = fgetc(file)) != -1)
printf("%c", c);
return 0;
}
a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
View Answer
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
Explanation:None.
Output:
$ cc pgm3.c
$ a.out
a
8. What is short int in C programming?
a) Basic datatype of C
b) Qualifier
c) short is the qualifier and int is the basic datatype
d) All of the mentioned
View Answer
a) Basic datatype of C
b) Qualifier
c) short is the qualifier and int is the basic datatype
d) All of the mentioned
View Answer
Answer:c
Explanation:None.
Explanation:None.
SET 2
1. Comment on the output of this C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1)
printf("equal\n");
else
printf("not equal\n");
}
a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answerb) not equal
c) Output depends on compiler
d) None of the mentioned
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
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. Comment on the output of this C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1f)
printf("equal\n");
else
printf("not equal\n");
}
a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answerb) not equal
c) Output depends on compiler
d) None of the mentioned
Answer:a
Explanation:0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
Explanation:0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
3. What is the output of this C code (on a 32-bit machine)?
#include <stdio.h>
int main()
{
int x = 10000;
double y = 56;
int *p = &x;
double *q = &y;
printf("p and q are %d and %d", sizeof(p), sizeof(q));
return 0;
}
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 Answerb) p and q are 4 and 8
c) Compiler error
d) p and q are 2 and 8
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
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 size of the datatypes?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
View Answera) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
Answer:c
Explanation:char has lesser bytes than int and int has lesser bytes than double in any system
Explanation:char has lesser bytes than int and int has lesser bytes than double in any system
5. What is the output of the following C code(on a 64 bit machine)?
#include <stdio.h>
union Sti
{
int nu;
char m;
};
int main()
{
union Sti s;
printf("%d", sizeof(s));
return 0;
}
a) 8
b) 5
c) 9
d) 4
View Answerb) 5
c) 9
d) 4
Answer:d
Explanation:Since the size of a union is the size of its maximum datatype, here int is the largest hence 4.
Output:
$ cc pgm7.c
$ a.out
4
Explanation:Since the size of a union is the size of its maximum datatype, here int is the largest hence 4.
Output:
$ cc pgm7.c
$ a.out
4
6. What is the output of this C code?
#include <stdio.h>
int main()
{
float x = 'a';
printf("%f", x);
return 0;
}
a) a
b) run time error
c) a.0000000
d) 97.000000
View Answerb) run time error
c) a.0000000
d) 97.000000
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
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 datatypes have size that is variable?
a) int
b) struct
c) float
d) double
View Answera) int
b) struct
c) float
d) double
Answer:b
Explanation:Since the size of the structure depends on its fields, it has a variable size.
Explanation:Since the size of the structure depends on its fields, it has a variable size.
VARIABLES
1. C99 standard guarantees uniqueness of ____ characters for internal names.
a) 31
b) 63
c) 12
d) 14
Answer:b
Explanation:ISO C99 compiler may consider only first 63 characters for internal.
2. C99 standard guarantess uniqueness of _____ characters for external names.
a) 31
b) 6
c) 12
d) 14
Answer:a
Explanation:ISO C99 compiler may consider only first 31 characters for external
variables having 31 characters due to which it may not be unique.
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
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
Answer:c
Explanation:Variable name cannot start with a digit.
5. 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
Answer:c
Explanation:None.
6. All keywords in C are in
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None
Answer:a
Explanation:None.
7. Variable name resolving (number of significant characters for uniqueness of variable) depends on
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None
Answer:a
Explanation:It depends on the standard to which compiler and linkers are adhering to.
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
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
Answer:c
Explanation:According to the syntax for C variable name, it cannot start with a digit.
SET 2
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;
Answer:b
Explanation:space, comma and $ cannot be used in a variable name.
2. What is the output of this C code?
#include <stdio.h>
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!
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 is the output of this C code?
#include <stdio.h>
int main()
{
int y = 10000;
int y = 34;
printf("Hello World! %d\n", 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
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
Answer:d
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.
5. What will happen if the below program is executed?
#include <stdio.h>
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
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?;
a) The variable name begins with an integer
b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned
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. Comment on the output of this C code?
#include <stdio.h>
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
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.
---------------------------------------------------------------------------------------------------------------------