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

Solutions To Assessment: Basic Programming Constructs: 1. Int A, B, C, D, e

The document provides solutions to programming questions on basic programming constructs like loops, conditional statements, functions etc. It includes 14 questions with detailed explanations and test cases for each question. The questions cover topics like finding the largest number among inputs, checking if points lie on a straight line, calculating digital root of a number, printing factors of a number.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Solutions To Assessment: Basic Programming Constructs: 1. Int A, B, C, D, e

The document provides solutions to programming questions on basic programming constructs like loops, conditional statements, functions etc. It includes 14 questions with detailed explanations and test cases for each question. The questions cover topics like finding the largest number among inputs, checking if points lie on a straight line, calculating digital root of a number, printing factors of a number.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Solutions to Assessment: Basic Programming Constructs

Question 1:
Specify the minimum number of comparisons required to find the largest number among a set of 3
integers X, Y and Z.
Solution:
We already know how to find the largest number between X, Y and Z using 2 comparisons. Using 1
comparison, we can compare only two numbers; so there is no way of telling the largest among X, Y, Z
using just 1 comparison. Therefore, the answer is 2
Ans: 2
Question 2:
Specify the minimum number of comparisons required to find the largest number among a set of 4
integers W, X, Y and Z.
Solution: We need N-1 comparisons in general to find the minimum of N numbers.
Ans: 3
Question 3:
What is the value of variable a at the end of the following program segment?
1. int a,b,c,d,e;
2. a = 6;
3. b = 3;
4. c = -2;
5. d = 2;e = 4;
6. a = a * b;
7. d = d * 2;
8. a = a + c * d / e;
9. a = a - 5;
Solution:
Value of variable a after execution of line 6 is 18
Value of variable d after execution of line 7 is 4
Line 8 can be re-written as a = (a + ((c*d)/e)), because operators * and / have an equal precedence,
which is higher than +
Value of variable a after execution of line 8 is 16
Value of variable a after execution of line 9 is 11
Ans: 11
Question 4:
What is the value of variable a at the end of the following program segment?

b = 10;
a = b++;
Solution:
The expression b++ increments the value of the variable b to 11, but the expression b++ evaluates
to the value 10. This value is assigned to the variable a.
Ans: 10
Note: ++ in the expression b++ is called post-increment operator. Operator ++ can also be written
before a variables name (Eg: ++b). In that case, its called pre-increment operator and the expression
++b evaluates to 11
Question 5:
Which among the following operators has the highest precedence?
a.) * b) == c) & d)
+
Solution:
The order of precedence for the operators in descending order is as follows:
*, +, ==,&
Ans: a
Question 6:
What is the output of the following code segment?
int x=10;
int y=5;
printf("%d",(y,x));
Solution:
In the expression (y, x) comma acts as an operator. The expression (y, x) evaluates to the second value,
i.e. x. Thus the value of variable x is printed.
Please see http://en.wikipedia.org/wiki/Comma_operator#Examples for more examples
Ans: 10
Question 7:
What is the output of the following code segment?
int x=1000;
int y=5000;
printf("%d",y,x);
Solution:
In this question, comma acts only as separator. printf() statement prints the value of the variable y and
ignores the variable x

Ans: 5000
Question 8:
What is the output of the following code segment?
int a=10,b=200;
int c;
c=a,b;
printf("%d",c);
Solution:
c = a, b; is equivalent to (c = a), b; so the variable c is assigned the value of variable a. Therefore the
answer is 10.
Please see http://en.wikipedia.org/wiki/Comma_operator#Examples for more examples
Ans: 10
Question 9:
Consider the following code snippet. What is the output if x is 10?
switch(x)
{
default: printf(x);
case 10: printf(y);
case 20: printf(z);
}
Solution:
In switch statement, code under a matching label is first executed, and the execution continues till the end
of switch.
When x = 10; case 10: block is executed and then case 20:
Ans: yz
Question 10:
What is the output of the following code snippet?
1. int x=10;
2. if (x==5)
3. printf("5");
4. if (x%5==0)
5. printf("10");
6. else
7. printf("bye");
Solution:

In line 2, x == 5 evaluates to 0 (or False), line 3 doesnt get executed.


In line 4, x % 5 == 0 evaluates to 1(or True), statement in line 5 is executed. else in line 6 is not
executed obviously because if statement was already true.
Ans: 10
Question 11:
What is the output of the following code snippet?
1. int x=5;
2. if ( x < 10 )
3. printf( "<10" );
4. else if ( x % 2 == 0 )
5. printf( "even" );
6. else if ( x >= 5)
7. printf(">=5" );
8. else
9. printf( "odd and <=5");
Solution:
In if else if else construct, only the first true statement will be executed.
x < 10 in Line 2 evaluates to True, Line 3 will be executed.
Ans: <10
Question 12:
Is the program segment below valid?
for(;;);

Solution:
The statement above is a valid C statement
Ans: Yes
Question 13:
What is of the following is the output of the following code segment?
int i;
for(i=1;i++<=5;printf(%d,i));
Solution:
for(variable initialization; condition; variable update){
Code to execute while the condition is true
}
For the first iteration, condition is executed and if it evaluates to True, the body of for loop is executed.
For the subsequent iterations, statement(s) in variable update is/are executed, followed by statement(s)

in condition. If condition evaluates to True, body of the for loop is executed.


Also note that, if i = 5, the statement i++ evaluates to 5; and hence i++ <= 5 evaluates to True
Ans: 23456
Question 14:
What is the output of the following code segment?
int i=1;
while(i++<5);
printf("%d",i);
Solution:
The while loop starts with the check (1 < 5) but it gets post-incremented to 2. This loop continues till i =
5. When you check (i++ < 5), it evaluates (5 <5) which is FALSE and the loop exits. However, because of
i++, i gets incremented to 6. The result printed would be 6.
Ans: 6
Question 15:
What is the output of the following code segment?
1. int i=1;
2. do
3.
while(i++<=5);
4. while(i++<=4);
5. while(i++<=3);
6. printf(%d,i);
Solution:
Just like in Q15, the while loop in Line 3, would leave i with the value 7.
The check in line 4, checks (7<=4) but also increments it to 8. Finally, the check in line 9 fails but the
value of i gets incremented to 9. Thus 9 is the final answer.
Ans: 9

Solutions to Programming Assignments


Question 1:
Write a program that reads numbers which are in the range 0 to 100, till it encounters -1. Print the sum of
all the integers that you have read before you encountered -1

INPUT:
A sequence of integers separated by whitespace. There may be other integers following -1.
OUTPUT:
Sum of all integers in the sequence before you encounter -1. Any integer that is input after -1 in
the sequence should be ignored.
CONSTRAINTS:
Atmost 10 integers will be given in the input. One of them is guaranteed to be a -1.
Inputs will be in the range 0 to 100 (both included).
Solution:
#include<stdio.h>
int main()
{
int sum=0;
int number=0;
do
{
sum=sum+number; //update partial sum
scanf("%d",&number); //read a number from the input
}while(number!=-1); //check whether recently added number is -1 or not
printf("%d",sum); //print the final sum
return 0;
}
Public test cases:
Input

Output

2 -1 2 3

-1 4 5

10 3 4 -1

17

2 2 3 4 5 6 -1 7 8 9

22

Private test cases:


Input

Output

-1

0 0 0 2 -1 4 99

2 3 4 -1

9 3 2 8 -1

22

5 7 -1 9

12

11 -1

11

1 2 3 4 5 6 7 8 9 -1

45

0 0 0 0 0 -1

Question 2:
Solution described in the video.
Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on
one straight line.
INPUT:
Six integers x1, y1, x2, y2, x3, y3 separated by whitespace.
OUTPUT:
Print Yes if all the points fall on straight line, No otherwise.
CONSTRAINTS:
-1000 <= x1, y1, x2, y2, x3, y3 <= 1000
Solution:
#include<stdio.h>
int main() {
int x1,y1,x2,y2,x3,y3;
//Reading all 6 integers from the input using scanf()
scanf("%d %d %d %d %d %d", &x1,&y1,&x2,&y2,&x3,&y3);
//Checking if the slopes (y2-y1)/(x2-x1) == (y3-y2)/(x3-x2). If they are equal, then all the
points lie on the same line.
//Instead of performing division in LHS and RHS, we cross multiply (x2-x1) and (x3-x2) to
handle the case when either (x2-x1) = 0 or (x3-x2) = 0.
if ((y2-y1)*(x3-x2) == (y3-y2)*(x2-x1)) {
printf("Yes");
} else {
printf("No");
}
return 0;
}
PUBLIC TEST CASES:

Input

Output

100030

Yes

-2 0 -2 1 -2 2

Yes

-62 14 -18 -23 -6 23

No

111213

Yes

PRIVATE TEST CASES:


Input

Output

71 44 -36 -47 60 -74

No

0 1 0 4 0 -10

Yes

-26 24 77 -44 -55 -28

No

8 -4 10 0 23 26

Yes

60 -9 -71 -57 2 -71

No

7 9 72 74 -20 -28

Yes

9 -1 23 13 422 412

Yes

10 30 -18 -54 23 69

Yes

Question 3:
The digital root (also called repeated digital sum) of a number is a single digit value obtained
by an iterative process of summing digits. Digital sum of 65536 is 7, because 6+5+5+3+6=25
and 2+5 = 7. Write a program that takes an integer as input and prints its digital root.
INPUT:
A single integer N
OUTPUT:
Digital root of the number N.
CONSTRAINTS:
1 <= N <= 10^7
PUBLIC TEST CASES:
Input

Output

65536

1214

9199999

PRIVATE TEST CASES:


Input

Output

99999

1233

1234

132224

239123

96999999

9077

887

9009

Solution: Check the video posted in the course webpage for a detailed explanation of the
following code:
#include<stdio.h>
int main()
{
int N, sum=0;
scanf("%d",&N);
while(N>9){
sum=0;
while(N>0){
sum += N%10;
N = N/10;
}
N = sum;
}

printf("%d",N);
return 0;
}

Question 4:
Write a program to print all the factors of a positive integer A.
INPUT:
A single integer A
OUTPUT:
Factors of the number A, in ascending order, separated by whitespace. 1 and A are also factors of A.
CONSTRAINTS:
2 <= A <= 10000
Solution:
#include<stdio.h>
int main()
{
int number;
int i=1;
scanf("%d",&number);
printf("%d",i);
for(i=2;i<=number;i++) //iterating over all numbers from 2 to number
{
if(number%i==0) //checking whether 'i' is a factor of number or not
{
printf(" %d",i); //printing a factor with a preceding whitespace
}
}
return 0;
}
PUBLIC TEST CASES:
Input

Output

1 2 3 6

8128

1 2 4 8 16 32 64 127 254 508 1016 2032


4064 8128

92

1 2 4 23 46 92

97

1 97

PRIVATE TEST CASES:


Input

Output

496

1 2 4 8 16 31 62 124 248 496

28

1 2 4 7 14 28

24

1 2 3 4 6 8 12 24

3234

1 2 3 6 7 11 14 21 22 33 42 49 66 77
98 147 154 231 294 462 539 1078 1617
3234

4006

1 2 2003 4006

2003

1 2003

30

1 2 3 5 6 10 15 30

1 2

You might also like