• edited by
18,435 views
33 33 votes

Consider the C program below. What does it print?

# include <stdio.h>
# define swap1 (a, b) tmp = a; a = b; b = tmp
void swap2 ( int a, int b)
{
        int tmp;
        tmp = a; a = b; b = tmp;
 }
void swap3 (int*a, int*b)
{
        int tmp;
        tmp = *a; *a = *b; *b = tmp;
}
int main ()
{
        int num1 = 5, num2 = 4, tmp;
        if (num1 < num2) {swap1 (num1, num2);}
        if (num1 < num2) {swap2 (num1 + 1, num2);}
        if (num1 > = num2) {swap3 (&num1, &num2);}
        printf ("%d, %d", num1, num2);
}
  1. $5, 5$
  2. $5, 4$
  3. $4, 5$
  4. $4, 4$

7 Answers

Best answer
41 41 votes

Answer is C.

Only:

if (num1 > = num2) {swap3 (&num1, &num2);}

Statement works, which in turn swaps num1 and num2.

• edited by
10 10 votes

In main program num1=5, num2=4

first condition true. num1=4, num2=5

Now, second condition become true but no affect on values still num1=4, num2=5

third condition fail.

Ans: 4,5

1 flag:
✌ Edit necessary (spongebob “wrong answer. 2nd statement says first condition true, but it isn't”)
1 1 vote
the swap1( ) function does not create new variables for a and b and temp. so it changes the same a and b. so they become 4 and 5.

swap2( ) function cant change the value of a and b because it is called by value and create different a and b.

the third condition does not get satisfied. so swap3( ) is not called and values remain as 4 and 5 for a and 5 respectively.
1 flag:
✌ Low quality (spongebob “Wrong Answer. first if does not execute as num1 is more than num 2”)
1 1 vote

The macro function gets subsituted by its defination. So, basically wherever in code swap1(a,b) is used it is replaced with 
tmp = a; a = b; b = tmp;


After substitution the code becomes:
 

int main(){

int num1 = 5, num2 = 4 , tmp;

if(num1 > num2){
    tmp = num1; num1 = num2; num2 = tmp;
}
...



So, first if condition (5 > 4) is satisfied . After swapping the values of num1 and num2 become 4, 5.

The second if condition(4 < 5 .) is satisfied. But as function call is by value, so num1 and num2 don't change.

The third if condition(4 >= 5) fails .

So, final answer is num1 = 4 , num2 = 5. (C)

1 flag:
✌ Edit necessary (spongebob “Wrong Answer. first if condition checks num1 less than num2, Not greater than”)
1 1 vote

Option C is the answer.

  • num1 = 5
  • num2 = 4
  1. First if statement: if(num1>num2)
    • 5 > 4 is true, so we execute swap1(num1, num2)
    • swap1 is a macro that expands to:
    • {
      int tmp = num1;
      num1 = num2;
      num2 = tmp;
      }
    • This successfully swaps the values
    • After swap1: num1 = 4, num2 = 5
       
  2. Second if statement: if(num1<num2)
    • 4 < 5 is true, so we execute swap2(num1 + 1, num2)
    • swap2 receives copies of the values (5 and 5)
    • This function swaps the copies but doesn't affect the original variables
    • After swap2: num1 = 4, num2 = 5 (unchanged)
       
  3. Third if statement: if(num1>=num2)
    • 4 >= 5 is false, so swap3 is not executed
       
  4. Thus, prints: 4, 5
1 flag:
✌ Edit necessary (spongebob “Wrong Answer. first if condition checks num1 less than num2, Not greater than”)
Answer:
Position:
Show:

Related questions

12 12 votes
2 answers 2 answers
7.5k
7.5k views
Ishrat Jahan asked Oct 27, 2014
7,483 views
Match the programming paradigms and languages given in the following table. Paradigms Languages(I)Imperative(a)Prolog(II)Object Oriented(b)Lisp(III)Functional(c)C, Fortra...
118 118 votes
10 answers 10 answers
42.2k
42.2k views
Ishrat Jahan asked Oct 31, 2014
42,245 views
Which one of the choices given below would be printed when the following program is executed?#include <stdio.h void swap (int *x, int *y) { static int *temp; temp = x; x ...
64 64 votes
5 answers 5 answers
19.7k
19.7k views
Ishrat Jahan asked Oct 28, 2014
19,672 views
C program is given below:# include <stdio.h int main () { int i, j; char a [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}}; char b [3] ; char *p = *b; for (i = 0; i < 2; i++)...
103 103 votes
4 answers 4 answers
25.6k
25.6k views
Ishrat Jahan asked Oct 28, 2014
25,602 views
Consider the C program given below. What does it print?#include <stdio.h int main () { int i, j; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8}; for(i = 0; i < 3; i++) { a[i] = a[i...