Tuesday, February 11, 2020

1.2. Fundamentals of programming language - C

SESSION-2 

1.The value of ab if ab & 0 x 3f equals 0 x 27 is 
A. 047 
B. 0 x 0 f 
C. 0 x f3 
D. 0 x 27 
ANSWER:
EXPLANATION- Let ab be 0 x MN. N&f should yield 7 i.e. N & 1111 should produce 0111. So, N should be 0111, i.e.,7.Similarly, M can be found to be 2. So, ab is 0 x 27. 

2. typedef float height [100]; 
height men, women; 
A. define men and women as 100 element floating point arrays 
B. define men and women as floating point variables 
C. define height, men and women as floating point variables 
D. are illegal 
ANSWER:
EXPLANATION: Assigning men and women as elements to the existing array typedef float height[10] 

3. The minimum number of temporary variables needed to swap the contents of two variables is 
A. 1 
B. 2 
C. 3 
D. 0 
ANSWER:
EXPLANATION: swapping doesn't necesssarily require variable 

4. Which of the following statement is correct prototype of the malloc() function in c ? 
A. int* malloc(int); 
B. char* malloc(char); 
C. unsigned int* malloc(unsigned int); 
D. void* malloc(size_t); 
ANSWER:
EXPLANATION: Any return type is possible and a size must be mentioned as a parameter while allocating memory 

5. 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 
ANSWER:
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. 

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 
ANSWER:
EXPLANATION: signed char will be a negative number. 

7. 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 
ANSWER:
Explanation:The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only. 

8. A utility programme that takes a procedure and searches a library to locate copies of any procedures called but not defined in the first procedure, is called 
A. Linker 
B. Re-locator 
C. Loader 
D. Text editor 
ANSWER:

9. What do the 'c' and 'v' in argv and argc stand for? 
A. 'c' means argument control 'v' means argument vector 
B. 'c' means argument count 'v' means argument vertex 
C. 'c' means argument count 'v' means argument vector 
D. 'c' means argument configuration 'v' means argument visibility 
ANSWER:

10. Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ? 
A. rem = 3.14 % 2.1; 
B. rem = modf(3.14, 2.1); 
C. rem = fmod(3.14, 2.1); 
D. Remainder cannot be obtain in floating point division. 
ANSWER:

11. Which header file should be included to use functions like malloc() and calloc()? 
A. memory.h 
B. stdlib.h 
C. string.h 
D. dos.h 
ANSWER:
EXPLANATION: malloc() and calloc() are library functions hence require stdlib.h 

12. 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 
ANSWER:
Explanation: typedef and struct are used to define user-defined data types. 

13. 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 
ANSWER:

14. 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 
ANSWER:
EXPLANATION: Size of any type of pointer is 4 on a 32-bit machine. 

15. 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 
ANSWER:
EXPLANATION: Since the size of a union is the size of its maximum datatype, here int is the largest hence 4. 

16. 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 
ANSWER: Since the ASCII value of a is 97, the same is assigned to the float variable and printed. 

17. Which of the datatypes have size that is variable? 
A. int 
B. struct 
C. float 
D. double 
ANSWER:
EXPLANATION: Since the size of the structure depends on its fields, it has a variable size. 

18. What is the output of this C code? 
#include <stdio.h> 
#define a 10 
int main() { 
const int a = 5; 
printf("a = %d\n", a); 
A. a = 5 
B. a = 10 
C. Compilation error 
D. Runtime error 
ANSWER:
Explanation: The #define substitutes a with 10 leaving no identifier and hence compilation error. 

19. What is the output of this C code? 
#include <stdio.h> 
enum birds {SPARROW, PEACOCK, PARROT}; 
enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
int main() { 
enum birds m = TIGER; 
int k; 
k = m; 
printf("%d\n", k); 
return 0; 
A. 0 
B. Compile time error 
C. 1 
D. 8 
ANSWER:
EXPLANATION: m is an integer constant, hence compatible. 

20. enum types are processed by 
A. Compiler 
B. Preprocessor 
C. Linker 
D. Assembler 
ANSWER:

21. What is the output of this C code? 
#include <stdio.h> 
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:
Explanation: Since the constant variable has to be declared and defined at the same time, not doing it results in an error. 

22. Comment on the output of this C code? 
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
ANSWER:
EXPLANATION: 0.1f results in 0.1 to be stored in floating point representations. 

23. Which of the following correctly represents a long double constant? 
A. 6.68 
B. 6.68L 
C. 6.68f 
D. 6.68LF 
ANSWER:

24. Comment on the output of this C code? 
#include <stdio.h> 
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:
EXPLANATION: Constant variable has to be declared and defined at the same time. Trying to change it results in an error. 

25. What is the output of this C code? 
#include <stdio.h> 
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 
ANSWER:
EXPLANATION: Cannot change a const type value. 

26. Comment on the output of this C code? 
#include <stdio.h> 
int main(){ 
const int i = 10; 
int *ptr = &i; 
*ptr = 20; 
printf("%d\n", i); 
return 0; 
A. Compile time error 
B. Compile time warning and printf displays 20 
C. Undefined behaviour 
D. 10 
ANSWER:
EXPLANATION: Changing const variable through non-constant pointers invokes compiler warning. 

27. What is the output of this C code? 
#include <stdio.h> 
int main(){ 
j = 10; 
printf("%d\n", j++); 
return 0; 
A. 10 
B. 11 
C. Compile time error 
D. 0 
ANSWER:
EXPLANATION: variable j is not defined

28. Which of the following declaration is not supported by C? 
A) String str; 
B) char *str; 
C) float str = 3e2; 
D) Both (a) and (c) 
ANSWER:
EXPLANATION: Legal in java, not in C 

29. Which is false ? 
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:
EXPLANATION: Variables can be declared and defined at different times 

30. What will the following program do? 
void main() { 
int i; 
char a[]="String"; 
char *p="New Sring"; 
char *Temp; 
Temp=a; 
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line number:9// 
p = malloc(strlen(Temp) + 1); 
strcpy(p,Temp);
printf("(%s, %s)",a,p); 
free(p); 
free(a); 
} //Line number 15//
A) Swap contents of p & a and print:(New string, string) 
B) Generate compilation error in line number 8 
C) Generate compilation error in line number 5 
D) Generate compilation error in line number 7 
E) Generate compilation error in line number 1 
ANSWER: C

31. 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:
EXPLANATION: space, comma, dollar cannot come while initializing or declaring an expression

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

33. 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: function and variable can have the same name

34. Which of the following cannot be a variable name in C? 
A) volatile 
B) true 
C) friend 
D) export 
ANSWER:
EXPLANATION: volatile is a keyword

35. Which of the following cannot be checked in a switch-case statement? 
A) Character 
B) Integer 
C) Float 
D) enum 
ANSWER: C
Explanation: float is imprecise due to it being inaccurate because of the rounding off errors which occur 

36. What does the following declaration mean? 
int (*ptr)[10]; 
A) ptr is array of pointers to 10 integers 
B) ptr is a pointer to an array of 10 integers 
C) ptr is an array of 10 integers 
D) ptr is an pointer to array 
ANSWER: B

37. The depth of a complete binary tree is given by 
A) Dn = n log2n 
B) Dn = n log2n+1 
C) Dn = log2n 
D) Dn = log2n+1 
ANSWER: D

38. What is the output of this C code? 
#include <stdio.h> 
int main() { 
enum {ORANGE = 5, MANGO, BANANA = 4, PEACH}; 
printf("PEACH = %d\n", PEACH); 
A) PEACH = 3 
B) PEACH = 4 
C) PEACH = 5 
D) PEACH = 6 
ANSWER: B
EXPLANATION: value of a constant is defined to the recent assignment to its left in enum.

39. In a 32-bit compiler, which 2 types have same size? 
A. char and short 
B. short and int 
C. int and float 
D. float and double 
ANSWER:

40. Which data type is suitable for storing a number like? 
10.0000000001 
A. int 
B. float 
C. double 
D. Both (b) and (c) 
ANSWER:
EXPLANATION: Double has a larger size than float 

41. What is the output of this C code? 
#include <stdio.h> 
int main(){
printf("%.0f", 2.89); 
}
A. 2.890000 
B. 2.89 
C. 2 
D. 3 
ANSWER:
EXPLANATION: it needs to be rounded of to a whole number as there is a criteria of not having any decimal point.

42. If integer needs two bytes of storage, then maximum value of an unsigned integer is 
A. 2^16 - 1 
B. 2^15 - 1 
C. 2^16 
D. 2^15 
ANSWER:

43. printf ( "%d" , printf ( "tim" ) ): 
A. results in a syntax error 
B. outputs tim3 
C. outputs garbage
D. prints tim and terminates abruptly 
ANSWER:
EXPLANATION: Any function (including main ( ) ) , returns a value to the calling environment. In the case of printf , it is the number of characters it printed. So. the output will be tim3 (since it printed the three characters a, b, c).

44. Literal means 
A. a string
B. a string constant 
C. a character 
D. an alphabet 
ANSWER: B

45. If a variable can take only integral values from 0 to n. where n is a constant integer, then the variable can be represented as a bit-field whose width is the integral part of (the log in the answers arc to the base 2) 
A. log (n) + 1 
B. log (n - 1) + 1 
C. log (n + 1) + 1 
D. none of the above 
ANSWER:
EXPLANATION: Let n = 7. It needs actually a 3 bit-field. But log (n + 1) + 1 will be log (8 ) +1, i.e., 4, which is wrong. If n = 8, 4 bits are needed. But, log (n - 1) + 1 will be log (7) + 1, which will have an integral part of 3.log (n) + 1 will yield the correct result in both the cases. 

46. The rule for implicit type conversion is 
A. int < unsigned < float < double 
B. unsigned < int < float < double 
C. int < unsigned < double < float 
D. unsigned < inc < double < float 
ANSWER: A 

No comments:

Post a Comment