PracticeQuestions ESC101 Summer

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

This sheet will be regularly updated by me with the practice problems for the respective

weeks.

Practice Questions (Tutorial 1)


Predict the output of the following programs?
Question 1
#include <stdio.h>
int main()
{
printf(" \"GEEKS %% FOR %% GEEKS\" ");
return 0;
}

Question 2
#include <stdio.h>
int main()
{
printf(5 + "GeeksQuiz");
return 0;
}
Question 3
#include <stdio.h>
int main()
{
int sum = 2 + 4 / 2 + 6 * 2;
printf("%d", sum);
return 0;
}
Question 4
#include <stdio.h>
int main()
{
int a = 3;
int res = a++ + ++a + a++ + ++a;
printf("%d", res);
return 0;
}
Question 5
#include <stdio.h>
int main()
{
int i = 2, j=2;
{
int i = 4, j = 5;
printf("%d %d\n", i, j);
}
printf("%d %d", i, j);
return 0;
}
Question 6
What is the output of this statement "printf("%d", (a++))"?
a. The value of (a + 1)
b. The current value of a
c. Error message
d. Garbage
Question 7
#include <stdio.h>
int main()
{
int num = 58;
num = num % 11;
printf("%d", num);
return 0;
}
Question 8
int main()
{
int a=10, b=20;
printf("a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d b=%d",a,b);
return 0;
}
Question 9
#include<stdio.h>
int main()
{
int a = 12;
switch(a)
{
case 15:
printf("Suzuki\n");
break;
case 10:
printf("APACHE\n");
break;
default:
printf("OlaElectric\n");
break;
}
printf("HEROHONDA");
return 0;
}
Question 10
int main()
{
char code='K';
switch(code)
{
case 'A': printf("ANT ");break;
case 'K': printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
}
Question 11
#include <stdio.h>
int main()
{
char code='A';
switch(code)
{
case 64+1: printf("ANT ");break;
case 8*8+4: printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
return 0;
}
Question 12
int main()
{
int k=64;
switch(k)
{
case k<64: printf("SHIP ");continue;
case k>64: printf("BOAT "); continue;
default: printf("PETROL");
}
printf("CHILLY");
}
Question 13
#include <stdio.h>
int main()
{
int s1 ,s3, num1, num2;
float s2;
num1 = scanf("%d %f %c", &s1, &s2, &s3);
num2 = printf("%d\n", num1);
printf("%d %d", num1, num2);
return 0;
}
Question 14
#include <stdio.h>
int main()
{
int a = 0, b = 5, c = 3;
b != !a;
c = !!a;
printf("%d\t%d", b, c);
}
Question 15
#include <stdio.h>
int main()
{
float x = -11.99;
printf("The number is %d",(int)x);
return 0;
}

Solutions (Tutorial 1)

Question Answer Explanation


1 "GEEKS % FOR % GEEKS" Escape sequence ‘\’ and to print ‘%’ we
need to use again, i.e., ‘%%’
2 Quiz The ‘5’ present in the printf statement
shifted the starting point by 5 values
3 16 BODMAS
4 20 Increment operator
5 45 Local and global scope
22
6 (b) It will print the current value of ‘a’, and
now increment it for future
7 3 Modulus operator (finds remainder)
8 a=10 b=20 Swapping without using third variable
a=20 b=10
9 OlaElectric As 12 is not present in any of the cases, the
HEROHONDA control moves to the default case
10 KING PALACE Break statement is present inside case
11 ANT PALACE ASCII value of ‘A’ is 65
(Also, ambiguity is not supported in case in
switch case)
12 Compile Time Error Switch case does not supports ‘comparison
operator’ as well as ‘continue’ statement
13 Sample Input: scanf returns the number of inputs taken
5 by the user
2.0 printf returns the length of the string
W present inside it
Output:
33 1
14 5 0 Logical operators
( b is compared but is not changed, c is
using logical operator)
15 The number is -11 Type casting only cuts out the number after
the decimal, and does not rounds off.

Practice Questions (Tutorial 2)


Predict the output of the following programs?
Question 1
#include <stdio.h>
int main()
{
int i = -5;
int k = -i % -4;
printf("%d\n", k);
return 0;
}
(Try changing signs of ‘i’ and ‘4’ in the above code, to learn more)
Question 2
Fill in the blank to make the code run properly. (PALINDROME NUMBER)
#include <stdio.h>
int main()
{
int n, rev = 0;
printf("Enter the number: ");
scanf("%d", &n);

int num= n; //To store the original number in the variable num

//Reverse the number and store it in variable rev


while (n > 0)
{
rev = rev * 10 + ________;
n = n / 10;
}

// check if original number is same as reversed number or not


if (num == rev)
printf("%d is a palindrome number.", num);
else
printf("%d is not a palindrome number.", num);

return 0;
}
Question 3
Fill in the blank to make the code run properly. (FIBONACCI SERIES)
#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = ___;
t2 = _______;
nextTerm = _______;
}

return 0;
}
Question 4
Fill in the blank to make the code run properly. (PRIME NUMBERS)
#include <stdio.h>

int main() {

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

// 0 and 1 are not prime numbers


// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;

for (i = 2; i <= ____; ++i)


{
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0)
{
flag = 1;
break;
}
}

// flag is 0 for prime numbers


if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Question 5
Fill in the blank to make the code run properly. (ARMSTRONG NUMBERS)
#include <stdio.h>
int main() {
int num, originalNum, rem, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
rem = originalNum % 10;
result += ______________________;
originalNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}
Question 6
Fill in the blank to make the code run properly. (FACTORIAL OF A NUMBER)
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){

fact = ______;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Question 7
What will be the output of this C code?
#include<stdio.h>

int main()

int a = 0, i = 0, b;

for (i = 0; i < 5; i++)

a++;

if (i == 3)
break;

printf("%d",a);

return 0;

Question 8
What will be the output of this C code?
#include<stdio.h>
int main()
{
int a = 0, i = 0, b;
for (i = 0; i < 5; i++)
{
a++;
if (i == 3)
continue;
}
printf("%d",a);
return 0;
}
Question 9
What will be the output of this C code?
#include <stdio.h>
void m()
{
static int x = 5;
x++;
printf("%d\n", x);
}
void main()
{
m();
m();
}
Question 10
What will be the output of this C code?
#include <stdio.h>
#include <math.h>

int main()
{
printf("%f\n",sqrt(10.0));
printf("%f\n",exp(4.0));
printf("%f\n",log(4.0));
printf("%f\n",log10(100.0));
printf("%f\n",fabs(-5.2));
printf("%f\n",ceil(4.5));
printf("%f\n",floor(-4.5));
printf("%f\n",pow(4.0,.5));
printf("%f\n",fmod(4.5,2.0));
printf("%f\n",sin(0.0));
printf("%f\n",cos(0.0));
printf("%f\n",tan(0.0));
return 0;
}
(Kindly run this code to understand better)
Question 11
Complete the code to print the following pattern using while loop:
1
12
123
1234
12345
#include <stdio.h>
int main() {
int end = 5;
int i = 1;
while (i <= end)
{
int j = 1;

while (_____)
{
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;}
Question 12
Complete the code to print the following pattern using for loop:
1
12
123
1234
12345
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for rows
for (int i = 1; i <= ____; i++) {
// Inner loop for columns
for (int j = 1; j <= ____; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}
Question 13
What will be the output of the C program?
#include <stdio.h>
int x=4;
void m()
{
printf("%d\n",x);
}

void main()
{
m();
x = x + 1;
m();
}
Question 14
What will be the output of the C program?
#include <stdio.h>
int x = 5;
void main()
{
int x = 3;
printf("%d\n", x);
{
int x = 4;
}
printf("%d", x);
}

Question 15
What will be the output of the C program?
#include <stdio.h>

int main()
{
int x = -1;
if(x<2)
{
int x = 1;
printf("%d\n", x);
}
int x = 15;
printf("%d", x);
return 0;
}

Solutions (Tutorial 2)

Ques Answer Explanation


1 1 The modulus(%) operator takes the sign of the dividend.
2 rev = rev * 10 + n % 10; A palindrome is a number that is same when read either
from left to right or right to left.
To calculate the digit at one’s place, we use the modulus
operator.
3 t1 = t2; Logic for Fibonacci series is to add the previous two terms.
t2 = nextTerm;
nextTerm = t1+t2;
4 for (i = 2; i <= n/2; ++i) A prime number is only divisible by 1 and the number
itself, we check till the half of the given number, i.e., n/2.
5 result += rem* rem* rem; Armstrong number example:
153 = 1*1*1 + 5*5*5 + 3*3*3
6 fact = fact * i; Factorial example:
5! = 5*4*3*2*1
7 4 The loop runs for 4 times before the ‘break’ results in exit
from the loop.
8 5 The loop will run for 5 times, as continue only skips the
part written after it inside the loop body.
9 6 Observe that we are declaring a static integer variable, so
7 when the function is called again, it’s value will not be
reinitialized.
10 3.162278 Try running the code to understand <math.h> library.
54.598150
1.386294
2.000000
5.200000
5.000000
-5.000000
2.000000
0.500000
0.000000
1.000000
0.000000
11 while (j <= i) We need to run the inside while loop with this condition to
print the desired pattern.
12 for (int i = 1; i <= n; i++) { The same can be done using a for loop also, anything
for (int j = 1; j <= i; j++) { which can be done using one type of loop can be done
printf("%d ", j); using other type of loop.

13 4 Doing x=x+1 inside main() function will change the value of


5 x.
14 3 The first declaration is at the global scope, outside any
3 function, with the value 5.
The second declaration is inside the main function, with
the value 3. This declaration has block scope, meaning it is
only accessible within the main function.
The third declaration is inside a nested block, denoted by
the curly braces {}. This nested block creates a new scope.
The variable x is declared with the value 4 inside this block,
and it is only accessible within this nested block.
15 Compile time error The code you provided will result in a compilation error.
This is because you are attempting to redeclare the
variable x multiple times within the same scope.

Practice Questions (Tutorial 3)


Question 1
What will be the output of the C program?
The number of swappings needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending order, using bubble sort is:
A) 11
B) 12
C) 13
D) 10

Question 2
Is the below logic correct to implement bubble sort(True or False)?

void bubbleSort(int arr[], int size)


{
int i, j;
for (i = 0; i < size - 2; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}

Question 3
What will be the output of the C program?

#include<stdio.h>
void 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);
}
Question 4
What will be the output of the C program?
The ______ function appends not more than n characters.
a) strcat()
b) strcon()
c) strncat()
d) memcat()

Question 5
What will be the output of the C program?
If the two strings are identical, then strcmp() function returns
A) -1
B) 1
C) 0
D) Yes

Question 6
What is this program meant to do?
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{
length++;
s++;
}
return (length);
}

Question 7
What will be the output of the following program?

#include<stdio.h>
#define square(x) x*x
void main()
{
int i;
i = 64/square(4);
printf("%d", i);
}

Question 8
What will be the output of the following program?
#include<stdio.h>
#define a 10
void main()
{
#define a 50
printf("%d", a);
}
A. 50
B. 10
C. Compiler Error
D. None of These

Question 9
What will be the output of the following program?

#include<stdio.h>
#define prod(a,b) a*b

void main()
{
int x=3,y=4;
printf("%d", prod(x+2,y-1));
}

Question 10
What will be the output of the following program?

#include<stdio.h>
#define max 5
void main(){
int i = 0;
i = max++;
printf("%d", i++);
}

Question 11
What will be the output of the following program?
#include<stdio.h>
int myshow(int *);

void main()
{
int a=10;
myshow(&a);
}

int myshow(int *k)


{
printf("Received %d, ", *k);
}

Question 12
What will be the output of the following program?
#include<stdio.h>
void myshow(int *);

void main()
{
int a=10;
printf("%d ", a);
myshow(&a);
printf("%d", a);

void myshow(int *k)


{
*k=20;
}

Question 13
What will be the output of the following program?
#include<stdio.h>
void myshow(int);

void main()
{
int a=10;
printf("%d ", a);
myshow(a);
printf("%d", a);
}

void myshow(int a)
{
a=20;
}

Question 14
What will be the output of the following program?
#include <stdio.h>
void display(int age1, int age2) {
printf("%d\n", age1);
printf("%d\n", age2);
}

int main() {
int ageArray[] = {2, 8, 4, 12};
display(ageArray[1], ageArray[2]);
return 0;
}
Question 15
What will be the output of the following program?
#include <stdio.h>
#include <stdlib.h>
void fun(int arr[]) // SAME AS void fun(int *arr)
{
unsigned int n = sizeof(arr)/sizeof(arr[0]);
printf("\nArray size inside fun() is %d", n);
}

int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Array size inside main() is %d", n);
fun(arr);
return 0;
}

Solutions (Tutorial 3)
Ques Answer Explanation
1 10 1) 8, 7, 9, 22, 5, 13, 31 = 4 swaps
2) 7, 8, 9, 5, 13, 22, 31 = 3 swaps
3) 7, 8, 5, 9, 13, 22, 31 = 1 swap
4) 7, 5, 8, 9, 13, 22, 31 = 1 swap
5) 5, 7, 8, 9, 13, 22, 31 = 1 swap
Total 10 swaps are required to sort the array.
NOTE: The number of passes and the number of swaps
required are two different things.
2 False for (i = 0; i < size - 1; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}
The first for loop should have ‘size-1' as the condition.
3 3, 2, 15 Just follow normal logic
4 (c) strncat() The strncat() function appends not more than n characters
from the array(s2) to the end of the string(s1)
5 (c) 0 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
6 Find length of the string ‘s’ The function calculates length of the string ‘s’
7 64 64/4*4 = 64
8 (A) 50 The preprocessor directives can be redefined anywhere in
the program. So the most recently assigned value will be
taken.
9 10 x+2*y-1 = x+(2*y)-1 = 10
10 Compile time error The error in the code is the use of the ++ operator on the
macro max. Macros are treated as constants and do not
behave like variables. The ++ operator can only be applied
to variables, not constants.
11 Received 10, It is called Passing a variable by reference. You are passing
&a instead of a. Address of a or &a is received as int *k.
Observe the function prototype declaration before main(),
int myshow(int *).
12 10 20 We passed ‘&a’ instead of a into myshow(int) function.
*k=20 changes the valued of passed variable passed by
reference.
13 10 10 We are passing only value of ‘a’, and hence new memory
in allocated for the ‘a’ present inside the function
myshow().
14 8 Logical, we are not passing the whole array, but just it’s
4 two elements
15 Array size inside main() is 8 In C, when we pass an array to a function say fun(), it is
Array size inside fun() is 2 always treated as a pointer by fun(). The below example
demonstrates the same.
// Note that arr[] for fun is just a pointer even if square
brackets are used.

You might also like