Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Programming and Data Structures  >  Previous Year Questions: Conditional Statement

Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Q1: Consider the following ANSI C program.
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following options is correct?  (2021 SET-1)
(a) The program will not compile successfully
(b)  The program will compile successfully and output 10 when executed
(c) The program will compile successfully and output 8 when executed
(d) The program will compile successfully and output 13 when executed
Ans:
(b)
Sol:

Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
From the above loop code, we can see that the loop iterates 7 times for
j∈ {-3, -2, -1, 0, 1, 2, 3}.
Now, we have an "if" condition and inside it, we have a logical AND operator (&&). In C language we have the following short-circuit rule for binary logical operators

  1. The second operand of logical OR operator || is ignored if the first operand is non-zero.
  2.  The second operand of logical and operator (&&) is ignored if the first operand is 0.

So, for j ∈ { -3, -2, -1} the first operand of && operator (j >= θ) will be 0, and hence the second operand (i++) will be ignored.
For j ∈ {0, 1, 2, 3} the first operand of && operator (j >= θ) will be 1, and hence the second operand (i++) will get evaluated 4 times and final value of i = 4.
Initial value of i = 0.
The postincrement operator i++, returns the original value of i and then increments i. So, when the first time i++ happens, the second operator of logical AND operator is 0 and hence the "if" condition fails. So, count = count +j happens only for j ∈ {1, 2, 3} and we get count = 0 + 1 + 2 + 3 = 6.
After the loop, we have count = count + i, which makes count = 6 + 4 = 10.
So, the correct option is B.

Q2: What is the output of the following program?  (2018)
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
(a) z2
(b) z1z2
(c) Compilation error
(d)None of these
And:
(d)
Sol: 
This code will first replace value of variable x by (5 + 2) = 7 because x < y. Then x will return it's current value 7.
So no output will be printed. i.e. option D is correct.

Q3: Assume A and B are non-zero positive integers. The following code segment:  (2018)
(a) Computes the LCM of two numbers
(b) Divides the larger number by the smaller number
(c) Computes the GCD of two numbers
(d) Finds the smaller of two numbers
Ans:
(c)
Sol: 
This is Euclidean algo for determining HCF of two numbers which works in this way:
If we subtract smaller number from larger (we reduce larger number), GCD doesn't change.
So if we keep subtracting repeatedly the larger of two, we end up with GCD.

Q4: What will be the output of the following C code?  (2017)
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 10 11 12 13 14
(b) 10 10 10 10 10
(c) 0 1 2 3 4 
(d) Compilation error
Ans: (b)
Sol:
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
Note for every time loop condition check with outer declare i and dor every outer declared i every time new variable i is declared and assign 10 to it.
so output will be 10, 10, 10, 10, 10,
At last value of outer i = 5 and inner i = 11

Q5: Consider the following segment of C-code:
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
The number of comparisons made in the execution of the loop for any n > 0 is:  (2016)
(a) [log2 n] + 1
(b) n
(c) [logn]
(d) [logn] + 1
Ans: 
(d)
Sol:

Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
We have to count those comparisons which happens during the execution of the loop and so the exit comparison must also be a part. So, the correct answer should be [log2 n] +2.
Since this is not in the choices we can assume that the question setter excluded the exit comparison and so the answer should be [logn] + 1.
Option D.

Q6: Consider the following program fragment  (2015)
 if(a > b)  if(b > c)     s1;  else s2;
s2 will be executed if
(a) a <= b
(b) b > c
(c) b >= c and a <= b
(d) a > b and b <= c
Ans: (d)
Sol: Point here to be noted is that when I write if (a > b) now after this I haven't written any braces so anything written within it would be considered as a single statement hence it is equivalent to if(a > b){ if (b < c) {s1;} else {s2;}
therefore clearly if s2 has to execute then we must have the condition a>b to be true simultaneously with b <= c, so option D is correct.

Q7: Consider the following C program:
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
The number of times printf statement is executed is ________.  (2015 SET-3)
(a) 10 
(b) 5 
(c) 7
(d) 8
Ans: (a)
Sol:  j=2 * 3 / 4 + 2.0 / 5 + 8 / 5;
j = (((2 * 3)/4) + (2.0/5)) + (8/5); //As associativity of +, * and / are from left to right and + has less precedence than * and /.
j = ((6/4) + 0.4) + 1); //2.0 is double value and hence 5 is implicitly typecast to double and we get 0.4. But 8 and 5 are integers and hence 8/5 gives 1 and not 1.6
j = (1 + 0.4) + 1; // 6/4 also gives 1 as both are integers
j = 1.4 + 1; //1 + 0.4 gives 1.4 as 1 will be implicitly typecast to 1.4
j = 2.4; // since j is integer when we assign 2.4 to it, it will be implicitly typecast to int.
So, j = 2;
k-= - - j;
This makes j = 1 and k = -1.
The variables j and k have values 1 and -1 respectively before the for loop. Inside the for loop, the variable i is initialized to 0 and the loop runs from 0 to 4.
i = 0, k = -1, i + k = -1, default case is executed, printf count = 1
i = 1, k = -1, i + k = 0, default case is executed, printf count = 2
i = 2, k = -1, i + k = 1, case 2, case 3 and default case is executed, printf count = 5
i = 3, k = -1, i + k = 2, case 2, case 3 and default case is executed, printf count = 8
i = 4, k = -1, i + k = 3, case 3 and default case is executed, printf count = 10
i = 5, loop exits and the control returns to main
 Answer is 10.

Q8: Consider the C program below.
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
The value printed by the above program is __________.  (2015 SET-2)
(a) 5
(b) 10
(c) 15
(d) 20
Ans:
(c)
Sol: 
Initially stack is empty = -1
stkFunc (-1, 10); this function
case -1: size = val; break; and static size= 10 // size memory declare one time only// and control comes out of switch b/c of break
stkFunc (0, 5); this function run
case 0: if (stkTop < size) A[stkTop++]=val; break; here stktop is static value so memory declare at compile time only now check if condition 0< 10 true then A[stktop++== A[0+1]=val= 5 i.e. push 5 into stack break comes so control comes outside
stkFunc (0, 10); this comes
case 0: if (stkTop < size) A[stkTop++]=val; break; same as above make A[stkTop++]= 10 i,e. push 10 into stack and break comes so control comes outside
printf ("%d\n", stkFunc (1, 0)+ stkFunc(1, 0));
this function
stkFunc(1, 0) this will run
default: if (stkTop) return A[--stkTop] return top of stack which is 10
stkFunc (1, 0) this will run
default: if (stkTop) return A[--stkTop] return top of stack which is 5
printf ("%d\n", stkFunc (1, 0) + stkFunc(1, 0));= 5+10=15 15 will be printed

Q9: What will be the output of the following C program segment?  (2012)
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
(a) No Choice
(b) Choice A
(c) Choice A 
Choice B No Choice
(d) Program gives no output as it is erroneous
Ans: (c)
Sol: 
Syntax:

  1.  switch(expression)
  2.  {
  3.  case constant-exp1: statements; break;
  4.  case constant-exp2: statements; break;
  5.  default: statements;
  6. }

In switch case, the break statement is used to terminate the switch case. Basically it is use
to execute the statements of a single case statement. If no break appears, the flow of control
will fall through all the subsequent cases until a break is reached or the closing curly brace '}' is reached.
In above , switch case don't have break statement ,
1. If inChar='A', flow control will execute all statements after Case A: till switch case end (in case any break states occurs between statement, it will terminated at the point but in above example we don't have any break statement, so whole switch block will execute).
Output: Choice A Choice B No Choice
In case if inChar='C' then output will be Choice B No Choice.
it's not necessary that we should always add break statement for each cases in switch, including break statement in switch case is based on our requirement.
Answer is C

Q10: What is the output of the following C code?  (2011)
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 1245
(b) 12345
(c) 12245
(d) 12354
Ans:
(b)
Sol: 
Here continue is last line . So, it will not skip any number

Q11: Consider the following pseudocode
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
What is the value of i at the end of the pseudocode?  (2011)
(a) 4
(b) 5 
(c) 6 
(d) 7
Ans: (b)
Sol:
Step: 1
x = 1, i = 1
Step: 2
x = 2, i = 2
Step: 3
x = 4, i = 3
Step: 4
x = 16, i = 4
Now, x = 16 < 500
Step: 5
x = 216, i = 5
∴ 5 is the right answer.

Q12: Consider the following code segment:
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
What is printed as a result of executing the code segment?  (2008)
(a) 4 16
(b) 4 10 16
(c) 0 6 12 18 
(d) 1 4 7 10 13 16 19
Ans: (b)
Sol:
k = 0 % 3 = 0 since k is incremented by 2
= 2%3 = 2
= 4%3 = 1------------prints 4
= 6%3 = 0
= 8%3 = 2
= 10%3 = 1-----------prints 10
=12%3 = 0
 ......... 
Therefore Output is 4  10  16

Q13: Let x be an integer which can take a value of 0 or 1. The statement
if (x = 0) x = 1; else x = 0;
is equivalent to which one of the following ?  (2004)
(a) x = 1 + x;
(b) x = 1 - x;
(c) x = x - 1;
(d) x = 1% x;
Ans: (b)
Sol: Firstly, our requirement is for x = 1 it makes '0' and for x = 0 it makes '1'
Let's consider options one by one:
A. x = 1 + x

  •  For x = 1, it gives 2 So, False

B. x = 1 - x

  • Here, B is correct, as
    • For x = 0, it gives 1.
    • For x = 1, it gives 0.

C. x = x - 1

  •  For x = 0, it gives -1. So, False

D. x = 1%x

  • For x = 0, it gives 1%0. I think it is undefined
  • Even if we consider x = x%1
    • for x = 0, it gives 0%1 = 0 But we require 1.

So, Option (B) is correct.

Q14: Given the programming constructs    
I. assignment
II. for loops where the loop parameter cannot be changed within the loop
III. if-then-else
IV. forward go to
V. arbitrary go to
VI. non-recursive procedure call
VII. recursive procedure/function call
VIII. repeat loop,
which constructs will you not include in a programming language such that it should be possible to program the terminates (i.e., halting) function in the same programming language  (1999)
(a) (II), (III), (IV)
(b) (V), (VII), (VIII)
(c) (VI), (VII), (VIII)
(d) (III), (VII), (VIII)
Ans:
(b)
Sol: 
This question is actually asking about the halting problem of Turing machines. Or in other words which of the constructs are needed to make a programming language Turing complete when it becomes Turing complete, halting problem becomes undecidable for it.
To start with if we only have a linear sequence of instructions it is guaranteed to terminate because we only have a finite number of instructions to execute and individual instructions can be assumed to finish within a finite time. This is similar to deciding if a TM halts within a finite number of steps or not which is decidable.
The problem (of deciding whether a program halts or not) comes when there is a loop. Again, not all loops are a problem as shown below.
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
Consider the above loop code. We can unroll the loop and repeat the loop body 100 times and what we get is a linear sequence of instructions. So, the above loop does not affect the decision of halting.
Well, in the above paragraph I did not specify one crucial requirement for unrolling the loop.
Assume we have a statement like
n = pow(n, x;)
where x is any program variable. Now, n is changing and so is the bound of the loop and we do not know how many times we have to unroll the loop. (This is similar to a Turing machine tape changing direction from right to left). Does this change make the halting decision undecidable now? "YES" it does. Because now whatever a Turing machine can do we can do in this programming language - Turing complete. So, if we can decide halting problem for this programming language we are indirectly solving the halting problem of Turing machines which is known to be unsolvable.
So now coming to the given constructs
1. assignment ✔
2. for loops where the loop parameter cannot be changed within the loop ✔
As described above this just translated to a finite number of sequential instructions
when unrolled. Some people might be confused with loops like
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
Here, if the loop body is not touching either i or n, the loop never terminates. But this decision (that it never terminates) can be decided easily by a written program (analyzing this is decidable and you can think of a C code to do it and equivalently we can have a Turing machine to decide this). So, the above loop even though being non-halting does not make the "halting decision" undecidable.
3. if-then-else ✔
 This just reduces one path (a set of instructions) from the linear sequence of instructions and hence does not affect the halting decision (assuming there are no other structures in either of the paths)
4. forward go to ✔
Like, if-else this also just eliminates some set of instructions.
5. arbitrary go to ✖
can simulate a for loop and so can cause problem in deciding halting.
6. non-recursive procedure call ✔
Each of the called procedure contributes to the number of executed instructions but since there is no recursion they'll eventually halt as long as each of the called procedures halt.
7. recursive procedure/function call ✖
This will also run into the same problem of a loop where the loop variables are changed inside the loop body. We may not be able to determine if the sequence of recursive calls ever terminates.
8. repeat loop ✖
Similar to a for loop if the looping condition is changed within the loop body this can make the halting decision undecidable.
Correct Option: B.

Q15:  The conditional expansion facility of macro processor is provided to  (1997)
(a) test a condition during the execution of the expanded program
(b) to expand certain model statements depending upon the value of a condition during the execution of the expanded program
(c) to implement recursion
(d) to expand certain model statements depending upon the value of a condition during the process of macro expansion
Ans: 
(d)
Sol: 
The conditional expansion facility of a macro processor is provided to expand certain model statements depending upon the value of a condition during the process of macro expansion.
This facility allows the macro processor to make decisions based on conditions and selectively include or exclude certain parts of the macro definition during the expansion process. It provides a way to customize the behavior of the macro expansion based on conditions specified in the source code or through parameters passed to the macro. This feature enhances the flexibility and adaptability of the macro processing mechanism.

Q16: Assume that X and Y are non-zero positive integers. What does the following Pascal program segment do?  (1995)
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
(a) Computes the LCM of two numbers
(b) Divides the larger number by the smaller number
(c) Computes the GCD of two numbers
(d)None of the above
Ans: 
(c)
Sol: 

take X = 6, Y = 4
if Ans will 12 then LCM, 2 then GCD, 1 then Divides the larger number by the smaller number
Now Trace the code
Iteration1 :- X = 2, Y = 4
2 :- X = 2, Y = 2
Print 2 ; which is GCD
take another pair X = 12, Y = 8 which gives 4 means ans is GCD.
Hence C is the Correct Ans

Q17: In the following Pascal program segment, what is the value of X after the execution of the program segment?  (1995)
Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 10
(b) -20
(c) -10
(d) None
Ans: 
(c)
Sol: 
The answer of X remains unchanged. As the if condition becomes false.
X := -10
The answer is C. This is a classic example of an if-else issue. Always else matches for nesting to the closest if in C Programming & Pascal.

Q18: An unrestricted use of the "goto" statement is harmful because  (1994)
(a) it makes it more difficult to verify programs
(b) it increases the running time of the programs
(c) it increases the memory required for the programs
(d) it results in the compiler generating longer machine code
Ans: 
(a)
Sol: Use of goto takes out the structural decomposition of the code and hence it becomes very difficult to verify or debug the code. As far as performance or memory impact is concerned, goto has no effect on them.
Correct Answer: A

The document Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Previous Year Questions: Conditional Statement - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a conditional statement in computer science?
Ans. A conditional statement in computer science is a programming construct that allows for different actions to be taken based on whether a certain condition is true or false. It typically uses keywords like "if," "else if," and "else" to control the flow of the program.
2. How do you write a basic if statement in programming?
Ans. A basic if statement in programming is written using the syntax: if (condition) { // code to be executed if the condition is true }
3. What is the difference between "if" and "else if" statements in programming?
Ans. An "if" statement is used to check a single condition, and if it is true, the code inside the block will be executed. An "else if" statement is used to check multiple conditions one by one until a true condition is found, and only the code inside the block of the first true condition will be executed.
4. Can you have multiple conditions in a single if statement?
Ans. Yes, you can have multiple conditions in a single if statement by using logical operators like "&&" (AND) and "||" (OR) to combine them.
5. What happens if none of the conditions in an if-else statement are true?
Ans. If none of the conditions in an if-else statement are true, the code inside the "else" block will be executed as a default fallback option.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Exam

,

Viva Questions

,

Extra Questions

,

Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)

,

Summary

,

Important questions

,

Objective type Questions

,

shortcuts and tricks

,

mock tests for examination

,

Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)

,

Free

,

past year papers

,

MCQs

,

ppt

,

video lectures

,

practice quizzes

,

pdf

,

Previous Year Questions: Conditional Statement | Programming and Data Structures - Computer Science Engineering (CSE)

,

Sample Paper

,

Previous Year Questions with Solutions

,

study material

,

Semester Notes

;