Sunteți pe pagina 1din 23

1. How many times the while loop will get executed if a short int is 2 byte wide? #include<stdio.

h> int main() { int j=1; while(j <= 255) { printf("%c %d\n", j, j); j++; } return 0; } A. Infinite times B.255 times

C.256 times

D.254 times

Answer: Option B Explanation: The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop 2. Which of the following is not logical operator? A. & B. && C.|| D.!

Answer: Option A Explanation: Bitwise operators: & is a Bitwise AND operator. Logical operators: && is a Logical AND operator. || is a Logical OR operator. ! is a NOT operator. So, '&' is not a Logical operator. 3. In Mathematics and computer programming, which is the correct order of mathematical operators? A. Addition, Subtraction, Multiplication, Division B. Division, Multiplication, Addition, Subtraction C. Multiplication, Addition, Division, Subtraction D. Addition, Division, Modulus, Subtraction Answer: Option B

Explanation: Simply called as BODMAS (Brackets, Order, Division, Multiplication, Addition and Subtraction).Mnemonics are often used to help students remember the rules, but the rules taught by the use of acronyms can be misleading. In the United States the acronym PEMDAS is common. It stands for Parentheses, Exponents, Multiplication, Division, Addition, Subtraction. In other English speaking countries, Parentheses may be called Brackets, or symbols of inclusion and Exponentiation may be called either Indices, Powers or Orders, and since multiplication and division are of equal precedence, M and D are often interchanged, leading to such acronyms as BEDMAS, BIDMAS, BODMAS, BERDMAS, PERDMAS, and BPODMAS. 4. Which of the following cannot be checked in a switch-case statement? A. Character Answer: Option C Explanation: The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value. The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.5. What will be the output of the program? #include<stdio.h> int main() { int i=0; for(; i<=5; i++); printf("%d,", i); return 0; } A.0, 1, 2, 3, 4, 5 B.5 C.1, 2, 3, 4 D.6 B. Integer C. Float D. enum

Answer: Option D Explanation: Step 1: int i = 0; here variable i is an integer type and initialized to '0'. Step 2: for(; i<=5; i++); variable i=0 is already assigned in previous step. The semi-colon at the end of this for loop tells, "there is no more statement is inside the loop". Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and then i is incremented by '1'(one)

Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and then i is increemented by '1'(one) Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and then i is incremented by '1'(one) Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is not incremented. Step 3: printf("%d,", i); here the value of i is 6. Hence the output is '6'. 6. What will be the output of the program? #include<stdio.h> int main() { char str[]="C-program"; int a = 5; printf(a >10?"Ps\n":"%s\n", str); return 0;} A.C-program Answer: Option A Explanation: Step 1: char str[]="C-program"; here variable str contains "C-program". Step 2: int a = 5; here variable a contains "5". Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as if(a > 10) { printf("Ps\n"); } else { printf("%s\n", str); } Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str. Hence the output is "C-program". 7. What will be the output of the program? #include<stdio.h> int main() { int x = 3; float y = 3.0; B. Ps C. Error D. None of above

if(x == y) printf("x and y are equal"); else printf("x and y are not equal"); return 0; C. Unpredictable D. No output

A. x and y are equal B. x and y are not equal Answer: Option A

Explanation: Step 1: int x = 3; here variable x is an integer type and initialized to '3'. Step 2: float y = 3.0; here variable y is an float type and initialized to '3.0' Step 3: if(x == y) here we are comparing if(3 == 3.0) hence this condition is satisfied. Hence it prints "x and y are equal".

8. What will be the output of the program? #include<stdio.h> int main() { int i = 1; switch(i) { printf("Hello\n"); case 1: printf("Hi\n"); break; case 2: printf("\nBye\n"); break; } return 0; } A. Hello B. Hello C.Hi D. Bye Hi Bye Answer: Option C Explanation: switch(i) has the variable i it has the value '1'(one). Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement. switch-case do not execute any statements outside these blocks case and default Hence the output is "Hi".

9. Point out the error, if any in the program #include<stdio.h> int main() { int a = 10; switch(a) { } printf("This is c program."); return 0; } A. Error: No case statement specified B. Error: No default specified C. No Error D. Error: infinite loop occurs Answer: Option C Explanation: There can exists a switch statement, which has no case. 9. Point out the error, if any in the while loop. #include<stdio.h> int main() { int i=1; while() { printf("%d\n", i++); if(i>10) break; } return 0; } A. There should be a condition in the while loop B. There should be at least a semicolon in the while C. The while loop should be replaced with for loop. D. No error

Answer: Option A Explanation: The while() loop must have conditional expression or it shows "Expression syntax" error. Example: while(i > 10){ ... }

10. Point out the error, if any in the program #include<stdio.h> int main() { int a = 10, b; a >=5 ? b=100: b=200; printf("%d\n", b); return 0; } A.100 B.200 C. Error: L value required for b D. Garbage value

Answer: Option C Explanation: Variable b is not assigned. It should be like: b = a >= 5? 100: 200; 11. If the two strings are identical, then strcmp() function returns A.-1 B.1 C.0 D. Yes

Answer: Option C Explanation: Declaration: strcmp(const char *s1, const char*s2); The strcmp return an int value that is if s1 < s2 returns a value < 0 if s1 == s2 returns 0 if s1 > s2 returns a value > 0

12. How will you print \n on the screen? A. printf("\n"); Answer: Option D Explanation: The statement printf("\\n"); prints '\n' on the screen. 13. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. B. C. D. The element will be set to 0. The compiler would report an error. The program may crash if some important data gets overwritten. The array size would appropriately grow. B. echo "\\n"; C. printf('\n'); D. printf("\\n");

Answer: Option C Explanation: If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors. 14. In C, if you pass an array as an argument to a function, what actually gets passed?

A. Value of elements in array B. First element of the array C. Base address of the array D. Address of the last element of array Answer & Explanation Answer: Option C Explanation: The statement 'C' is correct. When we pass an array as a function argument, the base address of the array will be passed.

15. 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: Option B 16. What will be the output of the program? #include<stdio.h> int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0; } A. 2, 1, 15 B. 1, 2, 5 C. 3, 2, 15 D. 2, 3, 20

Answer: Option C Explanation: Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 . Step 2: int i, j, m; The variable i,j,m are declared as an integer type. Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2 Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3. Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3) Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m Hence the output of the program is 3, 2, 15

17. How will you free the allocated memory ? A. remove(var-name); B. free(var-name); C. delete(var-name); Answer & Explanation Answer: Option B 18. What is the similarity between a structure, union and enumeration? A. B. C. D. All of them let you define new values All of them let you define new data types All of them let you define new pointers All of them let you define new structures D.dalloc(var-name);

Answer: Option B 19. What does fp point to in the program ? #include<stdio.h> int main() { FILE *fp; fp=fopen("trial", "r"); return 0; }

A. The first character in the file B. A structure which contains a char pointer which points to the first character of a file. C. The name of the file. D. The last character in the file. Answer: Option B Explanation: The fp is a structure which contains a char pointer which points to the first character of a file.

19. Which of the following operations can be performed on the file "NOTES.TXT" using the below code? FILE *fp; fp = fopen("NOTES.TXT", "r+"); A. C. Reading Appending B. D. Writing Read and Write

Answer: Option D Explanation: r+ Open an existing file for update (reading and writing).

20. To print out a and b given below, which of the following printf() statement will you use? #include<stdio.h> float a=3.14; double b=3.14; A. B. C. D. printf("%f %lf", a, b); printf("%Lf %f", a, b); printf("%Lf %Lf", a, b); printf("%f %Lf", a, b);

Answer: Option A Explanation: To print a float value, %f is used as format specifier. To print a double value, %lf is used as format specifier. Therefore, the answer is printf("%f %lf", a, b);

21. To scan a and b given below, which of the following scanf() statement will you use? #include<stdio.h> float a; double b; A. C. scanf("%f %f", &a, &b); scanf("%f %Lf", &a, &b); B. D. scanf("%Lf %Lf", &a, &b); scanf("%f %lf", &a, &b);

Answer: Option D Explanation: To scan a float value, %f is used as format specifier. To scan a double value, %lf is used as format specifier. Therefore, the answer is scanf("%f %lf", &a, &b); 22. Out of fgets() and gets() which function is safe to use? A. gets() B. fgets() Answer: Option B Explanation: Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored.

23. Consider the following program and what will be content of t? #include<stdio.h> int main() { FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0; } A. B. C. D. size of "DUMMY.C" file The handle associated with "DUMMY.C" file Garbage value Error in fileno()

Answer: Option B

Explanation: fp = fopen("DUMMY.C", "w"); A file DUMMY.C is opened in write mode and returns the file pointer to fp t = fileno(fp); returns the handle for the fp stream and it stored in the variable t printf("%d\n", t); It prints the handle number.

24. What is (void*)0? A. B. C. D. Representation of NULL pointer Representation of void pointer Error None of above

Answer: Option A Explanation: No answer description available for this question. Let us discuss. View Answer C Compiler Report Discuss in Forum

25. Can you combine the following two statements into one? char *p; p = (char*) malloc(100); A. char p = *malloc(100); B. char *p = (char) malloc(100); C. char *p = (char*)malloc(100); D. char *p = (char *)(malloc*)(100); Answer: Option C

26. In which header file is the NULL macro defined? A. stdio.h B. stddef.h C. stdio.h and stddef.h D. math.h

Answer: Option C Explanation: The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.

27.How many bytes are occupied by near, far and huge pointers (DOS)? A. near=2 far=4 huge=4 B. near=4 far=8 huge=8 C. near=2 far=4 huge=8 D. near=4 far=4 huge=8 Answer: Option A Explanation: near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long. 28. If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable? A. . B. & C. * D. ->

Answer: Option D 29. Which of the following is the correct order of evaluation for the below expression? z=x+y*z/4%2-1 A. */%+-= B. =*/%+C. /*%-+= D. *%/-+= Answer: Option A Explanation: C uses left associativity for evaluating expressions to break a tie between two operators having same precedence. 30. Which of the following correctly shows the hierarchy of arithmetic operations in C? A. / + * - B. *-/+ C. + - / * D. /*+Answer: Option D Explanation: Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction). How Do I Remember? BODMAS! B - Brackets first O - Orders (ie Powers and Square Roots, etc.) DM - Division and Multiplication (left-to-right) AS - Addition and Subtraction (left-to-right) 31. Which of the following is the correct order if calling functions in the below code? a = f1(23, 14) * f2(12/4) + f3(); A. f1, f2, f3

B. C. D.

f3, f2, f1 Order may vary from compiler to compiler None of above

Answer: Option C Explanation: Here, Multiplication will happen before the addition, but in which order the functions would be called is undefined. In an arithmetic expression the parenthesis tell the compiler which operands go with which operators but do not force the compiler to evaluate everything within the parenthesis first. 32. Which of the following are unary operators in C? 1. ! 2. sizeof 3. ~ 4. && A. 1, 2 B. 1, 3 C. 2, 4 D. 1, 2, 3 Answer: Option D Explanation: An operation with only one operand is called unary operation. Unary operators: ! Logical NOT operator. ~ bitwise NOT operator. sizeof Size-of operator. && Logical AND is a logical operator. Therefore, 1, 2, 3 are unary operators.

33. In which order do the following gets evaluated 1. 2. 3. 4. A. Relational Arithmetic Logical Assignment 2134 B. 1234 C. 4321 D. 3214

Answer: Option A Explanation: 1. Relational operators: >, <, >=, <=, ==, != 2. Arithmetic operators: *, /, %, +, -

3. Logical operators : !, &&, || 4. Assignment operators: = 34. What will be the output of the program? #include<stdio.h> int main() { int x=12, y=7, z; z = x!=4 || y == 2; printf("z=%d\n", z); return 0; } A. C. z=0 z=4 B. D. z=1 z=2

Answer: Option B Explanation: Step 1: int x=12, y=7, z; here variable x, y and z are declared as an integer and variable x and y are initialized to 12, 7 respectively. Step 2: z = x!=4 || y == 2; becomes z = 12!=4 || 7 == 2; then z = (condition true) || (condition false); Hence it returns 1. So the value of z=1. Step 3: printf("z=%d\n", z); Hence the output of the program is "z=1"

35. Assunming, integer is 2 byte, What will be the output of the program? #include<stdio.h> int main() { printf("%x\n", -2<<2); return 0; } A. ffff B. Answer: Option C 0 C. fff8 D. Error

Explanation: The integer value 2 is represented as 00000000 00000010 in binary system. Negative numbers are represented in 2's complement method.

1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0). 2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain the 2's complement value). Therefore, in binary we represent -2 as: 11111111 11111110. After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal system. 36. The maximum combined length of the command-line arguments including the spaces between adjacent arguments is A. 128 characters B. 256 characters C. 67 characters D. It may vary from one operating system to another Answer & Explanation Answer: Option D 37. Which of the following statements are FALSE about the below code? int main(int ac, char *av[]) { } A. B. C. D. ac contains count of arguments supplied at command-line av[] contains addresses of arguments supplied at a command line In place of ac and av, argc and argv should be used. The variables ac and av are always local to main()

Answer: Option C

38.In which numbering system can the binary number 1011011111000101 be easily converted to? A. Decimal system Answer: Option B Explanation: Hexadecimal system is better, because each 4-digit binary represents one Hexadecimal digit. 39. Which bitwise operator is suitable for turning off a particular bit in a number? A. && operator B. & operator C. || operator D. ! operator Answer: Option B B. Hexadecimal system C. Octal system D. No need to convert

40. Which bitwise operator is suitable for turning on a particular bit in a number? A. && operator B. & operator C. || operator D. | operator Answer & Explanation Answer: Option D Explanation: No answer description available for this question. Let us discuss. 41. Which bitwise operator is suitable for checking whether a particular bit is on or off? A. && operator B. & operator C. || operator D. ! operator Answer: Option B 42. Which of the following statements are correct about the program? #include<stdio.h> int main() { unsigned int num; int i; scanf("%u", &num); for(i=0; i<16; i++) { printf("%d", (num<<i & 1<<15)?1:0); } return 0; } A. It prints all even bits from num B. It prints all odd bits from num C. It prints binary equivalent num D. Error Answer: Option C Explanation: If we give input 4, it will print 00000000 00000100 ; If we give input 3, it will print 00000000 00000011 ; If we give input 511, it will print 00000001 11111111 ;

43. Which of the following statements are correct about the program? #include<stdio.h> int main() { unsigned int num; int c=0; scanf("%u", &num); for(;num;num>>=1) { if(num & 1) c++; } printf("%d", c); return 0; } A. B. C. D. It counts the number of bits that are ON (1) in the number num. It counts the number of bits that are OFF (0) in the number num. It sets all bits in the number num to 1 Error

Answer: Option A Explanation: If we give input 4, it will print 1. Binary-4 == 00000000 00000100 ; Total number of bits = 1. If we give input 3, it will print 2. Binary-3 == 00000000 00000011 ; Total number of bits = 2. If we give input 511, it will print 9. Binary-511 == 00000001 11111111 ; Total number of bits = 9. 44.What will be the output of the program? #include<stdio.h> int main() { int y=128; const int x=y; printf("%d\n", x); return 0; }

A. 128

B. Garbage value

C. Error

D.0

Answer: Option A Explanation: Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128". Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value. Step 3: printf("%d\n", x); It prints the value of variable 'x'. Hence the output of the program is "128"

45.What will be the output of the program? #include<stdio.h> #include<stdlib.h> union employee { char name[15]; int age; float salary; }; const union employee e1; int main() { strcpy(e1.name, "K"); printf("%s %d %f", e1.name, e1.age, e1.salary); return 0; } A. Error: RValue required B. Error: cannot convert from 'const int *' to 'int *const' C. Error: LValue required in strcpy D. No error Answer: Option D Explanation: The output will be (in 16-bit platform DOS): K 75 0.000000

46.What will be the output of the program? #include<stdio.h> int fun(int **ptr); int main() { int i=10; const int *ptr = &i; fun(&ptr); return 0; } int fun(int **ptr) { int j = 223; int *temp = &j; printf("Before changing ptr = %5x\n", *ptr); const *ptr = temp; printf("After changing ptr = %5x\n", *ptr); return 0; } A. Address of i Address of j B. 10 223 C. Error: cannot convert parameter 1 from 'const int **' to 'int **' D. Garbage value Answer: Option C Explanation: No answer description available for this question. Let us discuss.

47.What will be the output of the program? #include<stdio.h> int main() { const int x=5; const int *ptrx; ptrx = &x; *ptrx = 10;

printf("%d\n", x); return 0; } A. C. 5 Error B. D. 10 Garbage value

Answer: Option C Explanation: Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'. Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer. Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx. Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error. To change the value of const variable x we have to use *(int *)&x = 10; View Answer C Compiler Report Discuss in Forum 48.What will be the output of the program in TurboC? #include<stdio.h> int fun(int **ptr); int main() { int i=10, j=20; const int *ptr = &i; printf(" i = %5X", ptr); printf(" ptr = %d", *ptr); ptr = &j; printf(" j = %5X", ptr); printf(" ptr = %d", *ptr); return 0; } A. B. C. D. i= FFE2 ptr=12 j=FFE4 ptr=24 i= FFE4 ptr=10 j=FFE2 ptr=20 i= FFE0 ptr=20 j=FFE1 ptr=30 Garbage value

Answer: Option B 49. What will be the output of the program? #include<stdio.h> int main() { const char *s = ""; char str[] = "Hello"; s = str; while(*s) printf("%c", *s++); return 0; } A. Error Answer: Option C Explanation: Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string. Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello". Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello". Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the variable s is available and it prints the each character of the variable s. Hence the output of the program is "Hello". B. H C. Hello D. Hel

50.What will be the output of the program? #include<stdio.h> int get(); int main() { const int x = get(); printf("%d", x); return 0;

} int get() { return 20; } A. Garbage value Answer: Option C Explanation: Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns an integer value and accept no parameters. Step 2: const int x = get(); The constant variable x is declared as an integer data type and initialized with the value "20". The function get() returns the value "20". Step 3: printf("%d", x); It prints the value of the variable x. Hence the output of the program is "20". B. Error C. 20 D. 0

S-ar putea să vă placă și