0% found this document useful (0 votes)
19 views

Assignment4 July 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment4 July 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Problem Solving through Programming in C

Week 04 Assignment Solution

1. The control/conditional statements used in C is/are


a) if-else statements
b) switch statements
c) Both (a) and (b)
d) None of the above
Solution: (c) Both if-else and switch statements are conditional statements in C.

2. What is the output of the following C code?


#include <stdio.h>
int main()
{
int x = 1;
if (x == 0)
if (x >= 0)
printf("x=0\n");
else
printf("x=1\n");
return 0;
}

a) x=1
b) x=0
c) Depends on compiler
d) No print statements

Solution: (d) x is initialized with 1 and the if statement compares it with 0, thus the if condition is
false and the nested if statements do not get executed. Hence, the program does not print anything.

3. Compute the printed value of i of the C program given below


#include<stdio.h>
int main()
{
int i=2;
i=i++;
printf("%d", i);
return 0;
}

a) 2
b) 3
c) 4
d) Compiler error
Solution: (a) i++ is a post-increment operator. It assigns first and then increments the operator by one.
Therefore i value after the assignment remains 2

4. If multiple conditions are used in a single if statement then the testing of those conditions
are done
a) From Right to Left
b) From Left to Right
c) Randomly
d) None

Solution: (b) Multiple conditions are tested from Left to the right.
.
Problem Solving through Programming in C
Week 04 Assignment Solution

5. What is the purpose of the given program? n is the input number given by the user.
#include <stdio.h>
int main()
{
int n, x = 0, y;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
y = n % 10;
x = x - y;
n = n/10;
}
printf("Output is = %d", x);
return 0;
}

a) Sum of the digits of a number


b) The negative sum of the digits of a number
c) The reverse of a number
d) The same number is printed
Solution: (b) Negative sum of the digits of a number
Please take a number and follow the operation step-by-step. You will be able to find the
negative sum number as output.
6. while(1) is used in a program to create
a) False statement
b) Infinite loop
c) Terminating the loop
d) Never executed loop
Solution: (b) while(1) is used to create infinite loops.

7. What will be the value of a, b, c after the execution of the followings


int a=5, b=7, c=111;
c /= ++a * b--;

a) a=5, b=6, c=2;


b) a=6, b=7, c=1;
c) a=6, b=6, c=2;
d) a=5, b=7, c=1;

Solution: (c) ++a * b-- is computed as (a=a+1)*(b) 🡪 (6)*(7)=42


c/=42 🡪 c=c/42 🡪 c=111/42=2 (as c is integer)
Hence the right answer is a=6, b=6 and c=2
Problem Solving through Programming in C
Week 04 Assignment Solution

8. What will be printed when the following C code is executed?


#include<stdio.h>
int main()
{
if('A'<'a')
printf("NPTEL");
else
printf("PROGRAMMING");
return 0;
}

a) NPTEL
b) PROGRAMMING
c) No output
d) Compilation error as A and a are not declared as character variable
Solution: (a)
The ASCII value of ‘A’ is 65, which is lesser than the ASCII value of ‘a’ (i.e., 97). Therefore, if the
condition is true, NPTEL is printed.

9. Switch statement accepts


a) int
b) char
c) long
d) All of the above
Solution: (d) Integer, character and long constants are accepted in switch statements.

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


#include <stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1 \n");
default: printf("Choice other than 1 \n");
}
return 0;
}

a) Choice is 1
b) Choice other than 1
c) Both (a) and (b)
d) Syntax error
Solution: (c)
Since the “break;” statement is not used after the print statement, it will execute the default
instruction as well.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy