M3-R4: PROGRAMMIMG AND PROBLEM SOLVING THROUGH ‘C’
Q.1. Multiple Choice of Answers.
1.1 How many times below for loop will be executed?
#include<stdio.h>
int main()
{
int i=0;
for(;;)
printf("%d",i);
return 0;
}
A) 0 times
B) Infinite times
C) 10 times
D) 1 times
1.2 Find output of the following program?
#include<stdio.h>
int main()
{
char str[] = "Smaller";
int a = 100;
printf(a > 10 ? "Greater" : "%s", str);
return 0;
}
A) Greater
B) Smaller
C) Compile Error
D) 100
1.3 Which of the following cannot be checked in a switch-case statement?
A) Character
B) Integer
C) Float
D) enum
1.4 Difference between calloc() and malloc() is:
A) calloc() takes a single argument while malloc() needs two arguments
B) malloc() takes a single argument while calloc() needs two arguments
C) malloc() initializes the allocated memory to ZERO
D) calloc() initializes the allocated memory to NULL
1.5 What is the purpose of getc()?
A) read a character from STDIN
B) read a character from a file
C) read all file
D) read file randomly
1.6 Difference between structure and union is
A) We can define functions within structures but not within a union
B) We can define functions within union but not within a structure
C) The way memory is allocated
D) There is no difference
1.7 What is correct order of precedence in C?
A) Addition, Division, Modulus
B) Addition, Modulus, Division
C) Multiplication, Substration, Modulus
D) Modulus, Multiplication, Substraction
1.8 Disadvantage of array in C is
A) We can easily access each element
B) It is necessary to declare too many variables
C) It can store only one similar type of data
D) It is difficult to perform sorting operation on it
1.9 Which is invalid name of identifier?
A) world
B) addition23
C) test-name
D) factorial
1.10 Due to variable scope in C
A) Variables created in a function cannot be used another function
B) Variables created in a function can be used in another function
C) Variables created in a function can only be used in the main function
D) None of the above
Q.2. TRUE or FALSE.
2.1 Functions cannot return more than one value at a time.
TRUE
2.2 Functions can be called either by value or reference.
TRUE
2.3 Does the data type of all elements in the union will be same?
TRUE
2.4 We can allocate a 2-Dimensional array dynamically.
TRUE
2.5 The function free() is used to release dynamically allocated memory back to the heap for possible reuse.
TRUE
2.6 If p is a pointer then the expression p = p + 1; adds 1 to the value of p regardless of p's type.
FALSE
2.7 The string "Bilbo Baggins", may be stored in an array of 13 characters.
FALSE
2.8 typedef is used to give a new name to a known data type.
TRUE
2.9 The lifetime of a static variable is the duration of your program's execution.
TRUE
2.10 Are the three declarations char **apple, char *apple[], and char apple[][] same?
FALSE
Q.3. Match words and phrases
SR NO. | X | Y(Ans.) |
1 | Identifiers | Names that are given to various program elements, such as variables. |
2 | String constant | Any number of consecutive characters, enclosed in double quotation marks |
3 | getchar() | Enter a character from the standard input device |
4 | rand() | Return a random positive integer |
5 | putchar() | Send a character to standard output device |
6 | break() | Used to terminate loops or to exit from switch |
7 | continue | Used to bypass the remainder of the current pass through a loop |
8 | Automatic variables | Does not retain its value once control is transferred out of its defining function |
9 | int p(char *a[]) | Accepts an argument which is an array of pointers to characters |
10 | typedef | Allows user to define new data-types that are equivalent to existing data types |
Q.4. Fill in the blank
4.1 _pointer__ is a variable which holds the address of another variable.
4.2 A global variable is also known as ____external____ variable.
4.3 __int *p( char *a[ ] )___ accepts an argument which is an array of pointers to characters and returns a pointer to an integer quantity.
4.4 The argument list of function is known as function’s ____parameters____.
4.5 C provides the ____strcmp()____ function to compare strings.
4.6 ___atoi_____ converts a character string in the form of “whitespace sign digits” to an int value.
4.7 In compiler ___lvalue_____ error messages means that an object on left hand side of assignment operator is missing.
4.8 ____&&____ is logical AND operator and output of AND operation is TRUE if both the operands are true.
4.9 ___fseek()_____ sets the position to a desired point in the file.
4.10 ____union____, contains members whose individual data types may differ from one another.
Options.
A.int *p( char *a[ ] )
B.union
C.pointer
D.external
E.atoi
F.lvalue
G.rvalue
H.&&
I.ftell()
J.parameters
K.fseek()
L.strcmp()
M.int *(*p)(char(*a)[ ])
Long Questions
Q.5.
a) Write program to generate multiplication table for first ‘n’ number, where ‘n’ is a user input.
Ans:
The program below takes an integer input from the user and generates the multiplication tables up to 10.
Multiplication Table Up to 10
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
Output
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
b) Write a program to multiply two matrices.Ans:
Output:
Result matrix is
10 10 10 10
20 20 20 20
30 30 30 30
40 40 40 40
(7+8)Q.6.
a) Write program to compute factorial of a user input number using recursive function.
b) Write a program to write records of students in a file, where each record consists of student name, roll number, CGPA, and address. Read the records back from the file and display them.
c) Write difference between union and structure with example.
(5+7+3)
Q.7.
a) Write a program to input a set of numbers into an array. Pass the array to a function that finds and display the largest number.
b) Write in brief about storage classes in C.
(8+7)
Q.8.
a) Write a program using dynamic memory allocation to read numbers as input and display them in sorted order thereafter.
b) Write a program to concatenate two strings. (Do not use inbuilt string function).
(8+7)
Q.9.
a) Explain switch-case with a suitable example.
b) Write a program to generate the following patterns.
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
(6+9)
*****
Post a Comment
Leave a Reply