C Question Bank
C Question Bank
Page 1
UNIT –II : DECISION & LOOP CONTROL STATEMENTS
Short Answer (2 mark) Questions
(b) Write a program to find the sum of even and odd numbers from 1 to n.
8. (a) Write a program to find the factorial of a given number. [5+5]
(b) Write a program to generate ‗n‘ Fibonacci numbers.
9. (a) What is a nested loop? Write a program to display multiplications tables from 1 to n.
(b) Write a program to display the following pattern. [5+5]
*****
****
***
**
*
10. (a) Explain else-if ladder with the help of flowchart and program. [5+5]
(b) How does nested if-else works explain with an example?
Page 2
UNIT –III : Arrays and Functions
Short Answer (2 mark) Questions
Page 3
UNIT –IV : POINTERS AND STRINGS
Short Answer (2 mark) Questions
1. Define pointer. How can you declare it?
2. What is pointer to pointer?
3. What is pointer arithmetic?
4. Define pointer array.
5. How can you read a string through keyboard?
6. What is array of strings?
7. Display string ―pepper‖ in reverse order
8. Discriminate puts() and gets()
9. Discriminate putchar() and getchar()
10. How can you compare two strings?
Essay Answer (10 mark) Questions
2. (a) What are the features of pointers? Write a C program to print address of a variable
(b) Explain the declaration of pointers and pointer to pointer with examples. [6+4]
3. (a) With proper examples explain different arithmetic operations on pointers. [6+4]
(b) Write a C program to show that pointer of any data type occupies same space.
4. (a) Explain the concept of functions returning pointers with example. [5+5]
(b) Write a C program to read and print an array of elements using pointers.
5. (a) Explain the concept of array of pointers with examples. [4+6]
(b) Write a C program to read and display multiple strings using pointers.
6. (a) Write a C program to count the number of vowels, consonants, digits, spaces and
special characters in a given string. [5+5]
(b) Write a C program to read the elements in an array and print the same in reverse order.
7. (a) Write a C program to implement strcmp() , strcat() , strcpy() and strlen(). [5+5]
(b) Write a program to find the average marks obtained by a class of 50 students in a test.
8. (a) Explain declaration and initialization of array of strings. [4+6]
(b) Write a C program to find whether a given string is palindrome or not.
9. (a) Discus about arithmetic operations on characters. [5+5]
(b) Write a C program to read a set of strings and sort them in alphabetical order.
10. Explain the following string handling functions with example: [10]
a. strcpy() b. strcmp() c. strcat() d.strlen() e. strncat()
Page 4
UNIT –V : STRUCTURES AND FILE MANAGEMENT IN C
Short Answer (2 mark) Questions
1. (a) Define Structure and write the general syntax for declaring and accessing members.
(b) How to copy and compare structure variables? Illustrate with example. [5+5]
2. Write a C program that defines a structure employee containing the details such as empno,
empname, department name and salary. The structure has to store 20 employees in an
organization. Use the appropriate method to define the above details and define a function that
will display the contents? [10]
3. (a) Explain the following: [6+4]
i. Nested structures ii. Array of structures
(b) Write a C program to read and display student details using structure.
4. (a) Define union. Give the general template for union. [4+6]
(b) List out the differences between unions, structures and arrays
5. (a) How data elements are stored under unions, explain with example? [5+5]
(b) Write a C program to illustrate the concept of structure within structure.
6. (a) Write the syntax for opening a file with various modes and closing a file. [4+6]
(b) Explain the following file handling functions:
a. fseek() b. ftell() c. rewind() d. feof()
7. (a) Write a C program to display the contents of the file in reverse order. [5+5]
(b) Write a C program to copy the contents from one file to another file.
8. Write a C program to count no.of characters, spaces, lines, words of a file. [10]
9. (a) Discuss command line arguments in detail with examples. [5+5]
Page 5
(b) Write a short notes on
i. fgets() ii. fputs()
10. (a) Explain the following preprocessor directives: [4+6]
i. #include ii. #define
(b) Write a program in C that reads the name of a file and displays the contents of the file on the
user screen.
Page 6
UNIT-1 : OVERVIEW OF COMPUTERS AND C-PROGRAMMING
Page 7
positions.
(a) Two (b)Four (c) Six (d) Zero
21. What is the result of 8 | 4 ?
(a) 0 (b) 1 (c) 4 (d) 12
22. Which of the following operator is used to combine two or more relational expressions
(a) ^ (b) ~ (c) & (d) &&
23. ~(100111) gives
(a) 010010 (b) 011000 (c) 010100 (d) 111001
24. 10<<3 gives
(a) 40 (b) 1 (c) 80 (d) 30
25. Shifting a number ‗n‘ by ‗s‘ bits to left is equivalent to which of the following?
(a) 2s/n (b) n/2s (c) s2/n (d) n*2s
26. Shifting a number ‗n‘ by ‗s‘ bits to right is equivalent to which of the following?
(a) 2s/n (b) n/2s (c) s2/n (d) n*2s
27. Based on the precedence levels and associativity the 8+4*5+6/2 expression yields
(a) 43 (b) 34 (c) 31 (d) 41
28. operators are used for shifting bits to right and left
(a) >> and << (b) > and < (c) ?and : (d) None
29. The expression a++ is referred as
(a) Pre increment (b) Post increment (c) Before increment (d) After increment
30. The expression ++a referred as
(a) Pre increment (b) Post increment (c) Before increment (d) After increment
31. If a=3, b=5 the value of the expression ++a+b++ is
(a) 10 (b) 9 (c) 8 (d) None of the above
32. defines the order of evaluation when operators have the same precedence
(a) Priority (b) Precedence (c) Associativity (d) None of the above
33. Which one of the following is having highest precedence
(a) ++ (b) && (c) ( ) (d) ,
34. Which one of the following is having least precedence
(a) ++ (b) && (c) ( ) (d) ,
35. String constants are enclosed in
Page 8
UNIT-2 : DECISION & LOOP CONTROL STATEMENTS
{ int fruit=1;
switch(fruit+2)
{
default:printf("apple");
case 4: printf(" banana");
case 5: printf(" orange");
case 8: printf(" grape");
}
}
(a) applebanana orange grape (b) grape (c) orange (d) banana orange grape
14. Which for loop has range of similar indexes of 'i' used in for (i = 0;i< n; i++)?
(a) for (i = n; i>0; i–) (b) for (i = n; i >= 0; i–)
Page 9
(c) for (i = n-1; i>0; i–) (d) for (i = n-1; i>-1; i–)
15. What will be output when you will execute following C
code? void main()
{
int check=2;
switch(check)
{
case 2: printf("1");
break;
case 3: printf(" 2");
break;
}
}
(a) 12 (b) 2 (c) 1 (d) Compilation error
16. Which one among the following is the correct syntax of for loop?
(a) for(i=0;i<n;i++); (b) for(i<n;i=0;i++);
(c) for(i=0;i<n:i++); (d) None
17. ‗for‘ loop in C program , if the condition is missing
(a) assumed to be present and taken to be false
(b) assumed to be present and taken to be true
(c) syntax error
(d) execution will be terminated abruptly
18. if c is initialized to 1, how many times following loop is executed
While((c>0)&&(c<60))
{ c++; }
(a) 60 (b) 59 (c) 61 (d)1
19. The library function exit () causes an exit from
Page 10
23. What is the output of the following program
main()
{
int i;
for(i=1;i<5;i++)
{ if(i==3)
break;
Printf(―%d‖,i);
}
}
(a) 12345 (b)124 (c)1245 (d)12
24. What is the output of the following program
main()
{ int i;
for(i=1;i<5;i++)
{
if(i==3)
continue;
Printf(―%d‖,i);
}
}
(a) 12345 (b)124 (c)1245 (d)12
25. What are the entry controlled loops among the following
i. while ii. Do-while iii. For
(a) only i (b) only ii,iii (c) only iii (d) only i, iii
26. What is the output of the following program?
main()
{
int i=1;
while(i<=5)
printf(―%d‖,i);
}
(a) 12345 (b)1234 (c) 2345 (d) Leads to infinite loop
27. for(;;) can be terminated by
(a) break (b) exit(0) (c) return (d) All the above
28. What is the output of the following program
main()
{
for(i=1;i<=5;i++);
printf(―%d‖,i);
}
(a) 12345 (b)1234 (c) 6 (d) leads to infinite loop
29. What is the correct syntax of for loop
(a) for(i=0;i<n;i++){ } (b) for(i<n;i=0;i++){ }
(c) for(i=0;i<n:i++){} (d) for(i=0:i<n:i++){ }
30. Array is an example of which of the following?
(a) Derived types (b) Fundamental types (c) User-defined types (d) None
31. Which of the following is used to display a string on the screen?
Page 11
(a) %s (b) %c (c) %d (d) %f
32. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
(a) 10 (b) 9 (c) 0 (d) 1
33. Which of the following is exit controlled loop
(a) for (b) while (c) do-while (d) None
34. The default statement is executed when
(a) All the case statements are false (b) One of the case is true
(c) One of the case is false (d) None
35. How many times the following C code prints
―Hello‖ int main()
{
while (1)
printf("Hello ");
}
(a) One (b) zero (c) Infinite (d) Produce error
36. How many times the following C code prints
―Hello‖ int main()
{
do
{
printf("Hello ");
}while(0);
}
(a) One (b) zero (c) Infinite (d) Produce error
37. How many bytes the array price occupies. float price[10];
(a) 10 bytes (b) 4 bytes (c) 40 bytes (d) 20 bytes
38. Which of the following is syntactically correct?
(a) for(); (b) for(;); (c) for(,); (d) for(;;);
39. What is the output of the following code
main()
{
int a= 0,b = 20;
char x =1,y =10;
if(a,b,x,y)
printf("hello");
}
(a) Syntax error (b) hello (c) 10 (d) None
40. is used to terminate from the entire program
(a) return (b) break (c) exit (d) goto
Page 12
UNIT-3 : Arrays and Functions
1. Array is an example of which of the following?
(a) Derived types (b) fundamental types (c) user-defined types (d) None
2. Array elements are stored in
(a) Scattered memory locations (b) Sequential memory locations
(c) Direct memory locations (d) None
3. int a[10] will reserve how many locations in the memory?
(a) 10 (b) 9 (c) 11 (d) None of the above
4. Which one of the following is the correct syntax for initialization of one-dimensional arrays?
(a) int num[3]={0 0 0}; (b) int num[3]={0,0,0};
(c) int num[3]={0;0;0}; (d) int num[3]=0;
5. Under which of the following conditions, the size of the array need not be specified?
6. (a) When the compiler is smart (b) When initialization is a part of definition (c) Both
(d) None
7. Which of following is correct array declaration
A) int num(25); B) int array num[25]; C) int num[25]; D) num[25];
8. Array subscripts in ‗C‘ starts from
A) 0 B) compiler dependent C) 1 D) -1
9. Array elements are stored in
A) Column major order B) in diagonal order
C) Row major order D) either in row major or column major order
10. Which of the following statements is used to read a string of characters into the array words?
A) scanf(―%d‖, words); B) scanf(‖% \n‖, words);
C) scanf(“%s”, words); D) scanf(― %c‖, words);
int main()
{
int arr[]={12,13,14,15,16};
printf(―%d, %d, %d\n‖,sizeof(arr),sizeof(*arr),sizeof(arr[0]));
return 0;
}
a) 10, 2, 4 (b) 20, 4, 4 (c) 16, 2, 2 (d) 20, 2, 2
18. Which of the following statements correct about k used in the below statement?
char ****k;
(a) k is a pointer to a pointer to a pointer to a char
(b) k is a pointer to a pointer to a pointer to a pointer to a char
(c) k is a pointer to a char pointer
(d) k is a pointer to a pointer to a char
19. What will be the output?
main()
{
char *p;
printf(―%d %d‖,sizeof(*p),sizeof(p));
}
(a) 1 1 (b) 1 2 (c) 2 1 (d) 2 2
20. What will be the output?
main()
{
printf(―%d %d‖,sizeof(int *),sizeof(int **));
}
(b) 4 4 (b) 0 2 (c) 2 2 (d) 2 4
Page 16
21. Which one of the following is a string constant
(a) ‗3‘ (b) “hello” (c) 30 (d) None
22. Which of the following is used to display a string on the screen?
(a) %s (b) %c (c) %d (d) %f
23. Which of the following is used to determine the length of a string?
(a) strlen (b) strcmp (c) strcpy (d) strcat
24. Which of the following is the correct syntax for copying a string S1 into S2?
(a) strcpy(S2,S1); (b) strcpy(S1,S2); (c) strcmp(S1.S2); (d) strcmp(S2,S1);
25. The function strcat(S2,S1) appends to
(a) S1,S2 (b) S2,S1 (c) S2,S2 (d) S1,S1
26. Which of the following is used to read a string
(a) getchar() (b) gets() (c) getstr() (d) getch()
27. Which function is used to search for a substring in a string?
(a) strchr (b) strstr (c) strspn (d) strcpy
28. How many arguments that the strcmp() function can take?
(a) 2 (b) 3 (c) 4 (d) 0
29. What will be the result of the following character arithmetic expression?
X=‘A‘-2
(a) 63 (b) 64 (c) 65 (d) 66
30. Which of the following header file is required for performing string operations
(a) stdio.h (b) conio.h (c) string.h (d) ctype.h
31. Which function is used to count and return the number of characters in a given string
A) strcmp() B) strlen() C) strrev() D) strcat()
32. If the two strings are identical, then strcmp() returns
(a) -1 (b) 1 (c) 0 (d) yes
33. Which of the following function is more appropriate for reading in a multi-word string?
(a) printf() (b) scanf() (c) gets() (d) puts()
34. Which of the following not belongs to String functions?
A. strcmp() B. strcat() C.strlen() D. isdigit()
35. Which function is used to reverse the string?
(a) reverse() (b) strrev() (c) rev() (d) None
36. What will be the output of the
program? void main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%sn", strcpy(str2, strcat(str1, str2)));
}
(a) HelloWorld (b) World (c) WorldHello (d) Hello
37. What will be the output of the
program? void main()
{
char str[ ] = "online\0exam‖;
Page 17
printf(―%s‖,str);
}
(a) online\0exam (b) online (c) onlineexam (d) exam
38. String concatenation means
(a) Combining two strings (b) Extracting a substring out of a string
(c) Comparing two strings (d) partitioning the string into two strings
39. Which function locates the first occurrence of the character in a given string
(a) strstr() (b) strchr() (c) strrchr() (d) strrstr()
40. What is the output of the following code
main()
{ char str1[ ]=‖mahendra singh‖,str2[ ]=‖dhoni captain‖;
strncat(str1,str2,5);
printf(―\n %s‖,str1);
}
char c;
int x;
float y;
Page 18
}p;
How many bytes are allocated to union variable p?
A) 7 bytes B) 4 bytes C) 1 byte D) 2 bytes
10. In C language the Bit fields are used to
A) Save time B) Save memory
C) Change order of allocation of memory D) Save Program
11. The size of structure and union is same when they contain
A) Single member B) any number of members
C) Arrays of different types D) Pointers to different types
12. The operator used to find the size of any variable
A) sizeof() B) sizof() C) size of() D) size()
13. The operator is same as the combination of the operators
C) fp = fopen(―abc‖, ―w‖);
D) None of the mentioned
34. What does the following segment of code
do fprintf(fp, ―Copying!‖);
A) It writes “Copying!” into the file pointed by fp
B) It reads ―Copying!‖ from the file and prints on display
C) It writes as well as reads ―Copying!‖ to and from the file and prints it
D) None of the mentioned
35. FILE reserved word is
A) A structure tag declared in stdio.h
B) One of the basic datatypes in c
C) Pointer to the structure defined in stdio.h
D) It is a type name defined in stdio.h
36. Which of the following causes an error
A) Trying to read a file that doesn‘t exist
B) Inability to write data in a file.
C) Failure to allocate memory with the help of malloc
D) All of the menioned
37. fputs adds newline character
A) true B) false
C) Depends on the standard D) Undefined behavior
38. In fseek() function , the position value 2 indicates
A) Beginning of file B) End of file C) Current position D) All
39. Which of the following are C preprocessors?
A) #ifdef B) #define C) #endif D) All
40. In fseek() function , the position value 1 indicates
A) Beginning of file B) End of file C) Current position D) All
Page 20