Question Bank - CP
Question Bank - CP
Program: _First Year (All Branches) Engineering - SEM-II i--; Option B: 45.0 #include <stdio.h>
Curriculum Scheme: Rev 2019 } Option C: 45.000000 int main()
C-Programming return 0; Option D: 0.000000 {
Question Bank } 18 int a = 1, b = 1, c;
=========================================================== Option A: 2 12 Which among the following is a Conditional Operator in C ? c = a++ + b;
Choose the correct option for following questions. All the Option B: 3 Option A: ?: printf("a=%d, b=%d", a, b);
Q1. Questions are Option C: 4 Option B: :? }
compulsory and carry equal marks Option D: 5 Option C: <=
1. Which storage class is called as default storage class ? Option A: a=1, b=1
Option D: >=
Option A: auto 6. Option B: a=2, b=1
Which among the following is an exit controlled loop ?
Option B: register Option A: Option C: a=2, b=2
for What is the output of the C statement.?
Option C: static Option B: Option D: a=1, b=2
while int main()
Option D: extern Option C: do… while {
Option D: if…else int a=0; What will be the output of the following C code?
What inbuilt function should be used to return a value rounded up to the 13
2. a = 5<2 ? 4 : 3;
next higher integer ? 7 printf("%d",a); #include <stdio.h>
What is another name for 1-D arrays ?
Option A: floor void main()
Option A: Linear arrays return 0;
Option B: malloc } {
Option B: Lists
Option C: puts Option A: 4 int x = 5;
Option C: Horizontal array 19
Option D: ceil Option B: 3 if (x == 5)
Option D: Vertical array
Option C: 5 printf("hi\n");
In the following initialization what is value of A[5] ? int A[10] = {9, 8, 7, 6, 5, 8 Option D: 2 else
3. Which of the following operators takes only integer operands?
4, 3, 2, 1, 0}; Option A: printf("how are u\n");
+
Option A: 5 14 printf("hello\n");
Option B: * Recursion is a process in which a function calls _________.
Option B: 4 Option A: }
Option C: / itself
Option C: 3 Option A: hi
Option D: % Option B: another function
Option D: 2 Option C: hi
main() function Option B:
Option D: hello
What is value of a in following expression? sub program
What is the output for the following code ? 9 how are you
int a = 10 + 4.867; Option C:
int main() 15 hello
Option A: a=10 What is the Format specifier used to print a character in C.?
{ Option D: how are you
Option B: a=14.867 Option A: %s
int a=5,i; Option B:
4. Option C: a=14 %c
i!=a >10; What will be the output of the following C code? (Assuming that we have
Option D: a=4 Option C: %C
printf(“i=%d”,i); entered the value 1 in the standard input).
Option D: %w
return 0; #include <stdio.h>
10 C programs are converted into machine language with the help of ----------.
} void main()
Option A: an editor 16 Which of the following is not a relational operator?
Option A: i=0 {
Option B: an Assembler Option A: >=
Option B: i=10 int ch;
Option C: a compiler Option B: >>
Option C: i=110 printf("enter a value between 1 to 2:");
Option D: an operating system Option C: ==
Option D: i=1 scanf("%d", &ch);
Option D: != 20
switch (ch)
What is the output of the program.? {
How many times will the following while-loop repeat, i.e., how many x are
int main() 17 Which one of the following is a valid C expression? case 1:
printed? int main()
{ Option A: int my_number=1000; printf("1\n");
{
5. 11 float a = 45; Option B: int my-number=1000;
int i = 5; break;
printf("%f", a); Option C: int my@number=1000; printf("hi");
while(i> 0)
return 0; Option D: int @mynumber=1000; default:
{
} printf("2\n");
5. What are strings and give any four string related functions. for(j=0;j<n;j++) 8. Distinguish between structure and union. The syntax of while loop in c language is given below:
String is defined as character array. {
It is an array of elements with data type as “char” b[j][i]=a[i][j]; Structure Union while(condition)
It can be created using a keyword “struct” It can be created using a keyword “union”
In string, we can store all alphabets, all digits, space and special symbols } {
Example: } Statement
printf("\nTranspose Matrix=\n"); The total memory occupied by structure The total memory occupied by union }
char s1[10];
for(i=0;i<n;i++) variable is summation of memory occupied by variable is same as largest memory
If we want to handle multiple strings or names, we can create 2D string
{ individual structure elements occupied by union elements Flowchart:
Example:
char s1[5][10]; for(j=0;j<m;j++) It takes less memory as compared to
It takes more memory as compared to union
Following are different string handling functions which are present in header file “string.h” { structure
printf("%d ",b[i][j]); We can access individual elements We cannot access individual elements
} simultaneously simultaneously
Function Syntax (or) Example Description It is more efficient as compared to union It is less efficient as compared to structure
printf("\n");
} It takes less execution time as compared to It takes more execution time as compared
return 0; union to structure
strcpy() strcpy(string1, string2) Copies string2 value into string1
}
9. What are the tokens of c language explain with example.
strlen() strlen(string1) returns total number of characters in string1 7. Write a C program to find LCM of two numbers using recursion. Tokens in C is the most important element to be used in creating a program in C. We can
#include<stdio.h> define the token as the smallest individual element in C.
int GCD(int m,int n) There are 4 types of tokens:
strcat() strcat(string1,string2) Appends string2 to string1 1. Keywords:
{
if(n>m) Keywords are those words whose meaning is already known to the compiler
strrev() strrev(string1) It reverses the value of string1 { Example: include, void, main etc.
return GCD(n,m); 2. Variables and Constants:
} Variables are those entities whose value can change throughout the program
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same; else if(n==0) Constants are those entities whose value remain same throughout the program
less than 0 if string1<string2; greater than 0 if { 3. Identifiers:
string1>string2 return m; Identifiers are names given to variables or functions 11. Write a program to print Fibonacci series.
} Rules of Identifiers: #include<stdio.h>
else 1. It should not be keyword int main()
{ 2. It can contain all alphabets (A-Z and a-z), all digits (0-9) and special character underscore {
6. Implement a program to find transpose of a matrix. (_)
return GCD(n,m%n); int n,a,b,i,c;
#include<stdio.h>
} 3. It should either start with alphabet or underscore printf("Enter number of terms=");
int main()
} 4. Its maximum allowed length is 31 scanf("%d",&n);
{
int main() 5. It is case sensitive a=1;
int a[10][10],b[10][10];
{ 4. Operators: b=1;
int m,n,i,j;
int a,b,g,l; Operators are used to perform different operations printf("%d\t%d",a,b);
printf("Enter number of rows and columns of matrix=");
printf("Enter 2 numbers="); C contains following 3 categories of operators: for(i=1;i<=n-2;i++)
scanf("%d%d",&m,&n);
scanf("%d%d",&a,&b); 1. Unary Operators: {
for(i=0;i<m;i++)
g=GCD(a,b); It contains those operators which are used with single operand c=a+b;
{
l=(a*b)/g; 2. Binary Operators: printf("\t%d",c);
for(j=0;j<n;j++)
printf(“\nLCM=%d”,l); It contains those operators which are used with two operands a=b;
{
return 0; 3. Ternary Operators: b=c;
printf("\nEnter Number=");
} It contains those operators which are used with three operands }
scanf("%d",&a[i][j]);
return 0;
}
10. Explain while loop with example. }
}
while Statement:
for(i=0;i<m;i++)
It is entry-controlled statement. It first checks the condition and then execute the statement; the
{
process gets continued until the condition becomes false.
12. Write a program using recursion to find factorial of a number. scanf("%d%s%d%d%d",&s2.roll,&s2.name,&s2.mk.p,&s2.mk.c,&s2.mk.m); for(k=0;k<n1;k++) d=(a>b)?((a>c)?a:c):((b>c)?b:c);
#include<stdio.h> t1=s1.mk.p+s1.mk.c+s1.mk.m; { printf("Largest number=%d",d);
int fact(int n) t2=s2.mk.p+s2.mk.c+s2.mk.m; c[i][j]=c[i][j]+a[i][k]*b[k][j]; return 0;
{ if(t1>t2) } }
if(n==0) { }
{ printf("\nName of student having highest total=%s",s1.name); } 16. Explain the term recursion. Write a program to find the power of x raised
return 1; } } to n that is: xn, using recursive function.
} else int main() Recursion is the process of repeating items in a self-similar way. In programming languages, if a
else { { program allows you to call a function inside the same function, then it is called a recursive call
{ printf("\nName of student having highest total=%s",s2.name); int a[10][10],b[10][10],c[10][10],m1,n1,m2,n2; of the function.
return n*fact(n-1); } printf("Enter number of rows and columns of matrix 1=");
} return 0; scanf("%d%d",&m1,&n1); Program:
} } printf("Enter number of rows and columns of matrix 2="); #include<stdio.h>
int main() scanf("%d%d",&m2,&n2); int power(int x,int n)
{ 14. Write a C program to perform multiplication of two matrices. if(n1==m2) {
int n,f; #include<stdio.h> { if(n==0)
printf("Enter number="); void accept(int x[10][10],int m,int n) accept(a,m1,n1); {
scanf("%d",&n); { display(a,m1,n1); return 1;
f=fact(n); int i,j; accept(b,m2,n2); }
printf("\nFactorial=%d",f); for(i=0;i<m;i++) display(b,m2,n2); else
return 0; { multiply(a,b,c,m1,n1,m2,n2); {
} for(j=0;j<n;j++) printf("\nMultiplication Matrix=\n"); return x*power(x,n-1);
{ display(c,m1,n2); }
13. Explain nested structures with examples. printf("\nEnter Number="); } }
A nested structure in C is a structure within structure. One structure can be declared inside scanf("%d",&x[i][j]); else int main()
another structure in the same way structure members are declared inside a structure. We can } { {
access the member of the nested structure by } printf("\nMultiplication of matrix is not possible"); int x,n,p;
Outer_Structure_Variable.Inner_Structure_Variable.Inner_Structure_Element } } printf("Enter x and n=");
Example: return 0; scanf("%d%d",&x,&n);
#include<stdio.h> void display(int x[10][10],int m,int n) } p=power(x,n);
struct student { printf("\nResult=%d",p);
{ int i,j; 15. Explain conditional operator used in C language with proper example. return 0;
int roll; for(i=0;i<m;i++) }
char name[30]; { The conditional operator is also known as a ternary operator. The conditional statements are
struct marks for(j=0;j<n;j++) the decision-making statements which depends upon the output of the expression. It is
{ represented by two symbols, i.e., '?' and ':'. 17. Explain following functions with example
{
int p,c,m; printf("%d ",x[i][j]); sqrt(), fabs(), pow(), ceil(), floor()
} As conditional operator works on three operands, so it is also known as the ternary operator.
}mk;//Inner structure ka variable outer structure ka element maana jaata hai
}; printf("\n"); Function Description
} Syntax of a conditional operator
Expression1? expression2: expression3; returns the square root of given number.
int main() }
{ void multiply(int a[10][10],int b[10][10],int c[10][10],int m1,int n1,int m2,int n2)
Example: #include <stdio.h>
struct student s1,s2; {
#include<stdio.h> #include <math.h>
int t1,t2; int i,j,k;
int main() sqrt(n) int main ()
printf("Enter roll number,name and marks of physics,chemistry and maths of student for(i=0;i<m1;i++)
{ {
1="); {
int a,b,c,d; printf("Square root of %d is %f\n", 4, sqrt(4) );
scanf("%d%s%d%d%d",&s1.roll,&s1.name,&s1.mk.p,&s1.mk.c,&s1.mk.m); for(j=0;j<n2;j++)
printf("Enter 3 numbers="); printf("Square root of %d is %f\n", 5, sqrt(5) );
printf("\nEnter roll number,name and marks of physics,chemistry and maths of student {
scanf("%d%d%d",&a,&b,&c); return 0;
2="); c[i][j]=0;