C Programming Test 3
C Programming Test 3
C Programming Test 3
1.
Which statement will you add in the following program to work it correctly?
#include<stdio.h>
int main() { printf("%f\n", log(36.0)); return 0; }
A.
#include<conio.h>
B.
#include<math.h>
C.
#include<stdlib.h>
D.
#include<dos.h>
math.h is a header file in the standard library of C programming language designed for basic mathematical
operations. Declaration syntax: double log(double); Learn more problems on : Floating Point Issues Discuss about this problem : Discuss in Forum 2. What will be the output of the program?
A.
2, 3, 4,
B.
2, 2, 2,
C.
3, 3, 3,
D.
4, 4, 4,
Answer: Option A
Explanation: The macro PRINT(i) print("%d,", i); prints the given variable value in an integer format. Step 1: int x=2, y=3, z=4; The variable x, y, z are declared as an integer type and initialized to 2, 3, 4 respectively. Step 2: PRINT(x); becomes printf("%d,",x). Hence it prints '2'. Step 3: PRINT(y); becomes printf("%d,",y). Hence it prints '3'. Step 4: PRINT(z); becomes printf("%d,",z). Hence it prints '4'. Hence the output of the program is 2, 3, 4. Learn more problems on : C Preprocessor Discuss about this problem : Discuss in Forum 3. What will be the output of the program assuming that the array begins at the location 1002 and size of an integer is 4 bytes?
#include<stdio.h>
int main() { int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1)); return 0; }
A.
448, 4, 4
B.
520, 2, 2
C.
1006, 2, 2
D.
Error
Answer: Option C Learn more problems on : Pointers Discuss about this problem : Discuss in Forum 4. Which of the following statements correct about k used in the below statement?
char ****k;
A.
B.
C.
D.
Answer: Option B Learn more problems on : Pointers Discuss about this problem : Discuss in Forum 5. What does the following declaration mean?
int (*ptr)[10];
A.
B.
C.
D.
Answer: Option B Learn more problems on : Arrays Discuss about this problem : Discuss in Forum 6. What will be the output of the program in Turbo-C ?
#include<stdio.h>
int main() { int arr[5], i=-1, z; while(i<5) arr[i]=++i; for(i=0; i<5; i++) printf("%d, ", arr[i]); return 0; }
A.
1, 2, 3, 4, 5,
B.
-1, 0, 1, 2, 3, 4
C.
0, 1, 2, 3, 4,
D.
Answer: Option C Explanation: Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (under DOS) output. Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better. Learn more problems on : Arrays Discuss about this problem : Discuss in Forum 7. What will be the output of the program in 16-bit platform (Turbo C under DOS) ?
#include<stdio.h>
int main() { printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0)); return 0; }
A.
8, 1, 4
B.
4, 2, 8
C.
4, 2, 4
D.
10, 3, 4
sizeof(3.0f) is a floating point constant. The size of float is 4 bytes sizeof('3') It converts '3' in to ASCII value.. The size of int is 2 bytes sizeof(3.0) is a double constant. The size of double is 8 bytes
Hence the output of the program is 4,2,8 Note: The above program may produce different output in other platform due to the platform dependency of C compiler.
In Turbo C, 4 2 8. But in GCC, the output will be 4 4 8. Learn more problems on : Strings Discuss about this problem : Discuss in Forum 8. Point out the error in the program?
#include<stdio.h>
int main() { struct emp { char name[20]; float sal; }; struct emp e[10]; int i; for(i=0; i<=9; i++) scanf("%s %f", e[i].name, &e[i].sal); return 0; }
A.
B.
C.
No error
D.
None of above
Answer: Option B Explanation: At run time it will show an error then program will be terminated. Sample output: Turbo C (Windows) c:\>myprogram Sample 12.123 scanf : floating point formats not linked Abnormal program termination Learn more problems on : Structures, Unions, Enums Discuss about this problem : Discuss in Forum
9.
Answer: Option A Learn more problems on : Structures, Unions, Enums Discuss about this problem : Discuss in Forum 10. A pointer union CANNOT be created A. Yes B. No
Answer: Option B Learn more problems on : Structures, Unions, Enums Discuss about this problem : Discuss in Forum 11. What will be the output of the program if value 25 given to scanf()?
#include<stdio.h>
int main() { int i; printf("%d\n", scanf("%d", &i)); return 0; }
A.
25
B.
C.
D.
Answer: Option C Explanation: The scanf function returns the number of input is given.
printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one).
Therefore, the output of the program is '1'. Learn more problems on : Input / Output Discuss about this problem : Discuss in Forum
#include<stdio.h> #include<stdlib.h>
int main() { unsigned char; FILE *fp; fp=fopen("trial", "r"); if(!fp) { printf("Unable to open file"); exit(1); } fclose(fp); return 0; }
A.
B.
C.
No error
D.
None of above
Answer: Option C Explanation: This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program. If file exists, it simply close the file and then terminates the program. Learn more problems on : Input / Output Discuss about this problem : Discuss in Forum 13. What will be the output of the program (sample.c) given below if it is executed from the command line (turbo c under DOS)? cmd> sample Good Morning
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[]) { printf("%d %s", argc, argv[1]);
return 0; }
A.
3 Good
B.
2 Good
C.
Good Morning
D.
3 Morning
Answer: Option A Learn more problems on : Command Line Arguments Discuss about this problem : Discuss in Forum 14. What will be the output of the program (sample.c) given below if it is executed from the command line? cmd> sample friday tuesday sunday
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[]) { printf("%c", *++argv[2] ); return 0; }
A.
B.
C.
D.
Answer: Option C Learn more problems on : Command Line Arguments Discuss about this problem : Discuss in Forum 15. Even if integer/float arguments are supplied at command prompt they are treated as strings. A. True B. False
Answer: Option A Learn more problems on : Command Line Arguments Discuss about this problem : Discuss in Forum 16. If the different command line arguments are supplied at different times would the output of the following program change?
#include<stdio.h>
A.
Yes
B.
No
Answer: Option B Learn more problems on : Command Line Arguments Discuss about this problem : Discuss in Forum 17. What will be the output of the program?
#include<stdio.h>
typedef struct error {int warning, err, exception;} ERROR; int main() { ERROR e; e.err=1; printf("%d\n", e.err); return 0; }
A.
B.
C.
D.
Error
Answer: Option B Learn more problems on : Typedef Discuss about this problem : Discuss in Forum 18. What will be the output of the program?
#include<stdio.h>
int main() { const int i=0; printf("%d\n", i++); return 0; }
A.
10
B.
11
C.
No output
D.
Answer: Option D Explanation: This program will show an error "Cannot modify a const object". Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero). Step 2: printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object". Because, we cannot modify a const variable. Learn more problems on : Const Discuss about this problem : Discuss in Forum 19. Which standard library function will you use to find the last occurance of a character in a string in C? A. strnchar() B. strchar()
C.
strrchar()
D.
strrchr()
{
char str[30] = "12345678910111213"; printf("The last position of '2' is %d.\n", strrchr(str, '2') - str); return 0;
}
Output: The last position of '2' is 14. Learn more problems on : Library Functions
Discuss about this problem : Discuss in Forum 20. Data written into a file using fwrite() can be read back using fscanf() A. True B. False