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

Week-4-July-2023 Solution

The document contains solutions to 10 questions about if-else statements in C programming. Key points covered include: - The purpose of if-else is to test a condition and execute different code blocks based on the result - Correct syntax uses parentheses around the condition - Nested if-else statements allow testing of multiple conditions to execute different code blocks - If the else block is omitted, the code block will execute if the condition is true and nothing happens if false - Logical operators like && and || can be used to combine multiple conditions

Uploaded by

stephen neal
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)
56 views

Week-4-July-2023 Solution

The document contains solutions to 10 questions about if-else statements in C programming. Key points covered include: - The purpose of if-else is to test a condition and execute different code blocks based on the result - Correct syntax uses parentheses around the condition - Nested if-else statements allow testing of multiple conditions to execute different code blocks - If the else block is omitted, the code block will execute if the condition is true and nothing happens if false - Logical operators like && and || can be used to combine multiple conditions

Uploaded by

stephen neal
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

Week 4 Assignment Solution

1. What is the purpose of the "if-else" statement in C?


a) To execute a block of code repeatedly.
b) To declare variables and constants.
c) To test a condition and execute different code based on the result.
d) To perform mathematical calculations.

Solution: (c) To test a condition and execute different code based on the result.

2. What is the correct syntax for an "if-else" statement in C?


a) if condition { statement1; statement2; } else { statement3; }
b) if condition then { statement1; } else { statement2; }
c) if (condition) { statement1; } else { statement2; }
d) if condition then statement1; else statement2;

Solution: (c) if (condition) { statement1; } else { statement2; }

3. Which of the following is true about nested "if-else" statements?


a) They are not allowed in C.
b) The "else" part is mandatory for every "if" statement.
c) They allow you to test multiple conditions and execute different blocks of code
based on the results.
d) Nested "if-else" statements are only allowed up to two levels deep.

Solution: (c) They allow you to test multiple conditions and execute different
blocks of code based on the results.

4. What happens if there is no "else" part in an "if-else" statement?


a) The program will not compile.
b) The program will crash at runtime.
c) If the condition is true, nothing happens; if the condition is false, the program
crashes.
d) If the condition is true, the program executes the code inside the "if" block; if
the condition is false, nothing happens.

Solution: (d) If the condition is true, the program executes the code inside the "if"
block; if the condition is false, nothing happens.

5. Which of the following operators can be used to combine multiple conditions in an


"if" statement?
a) && (logical AND)
b) || (logical OR)
c) ! (logical NOT)
d) All of the above

Solution: (d) All of the above


Week 4 Assignment Solution

6. 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

7. 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 of the above

Solution: (b) Multiple conditions are tested from Left to the right.
8. 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
Week 4 Assignment Solution

Please take a number and follow the operation step-by-step. You will be able to find the
negative sum number as output.

9. 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

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