CS Code

Programmer Community

# Guess The Output Series 5 Tricky Questions

Guess the output series. This series contains 4 tricky question and answers with explanation.

## 1) Question

Some members guessed 12 or 11 as a output and some guessed there is an error in this program.

Don’t worry, guessing wrong is not a crime It’s a experience and we can learn something new from that experience

## Why the output is 3 ?

There are two blocks in main function including main function block.

First block contains

``````int a=1, b=2;
printf("%d",a);``````

Second block contains

``````a = a+b;
int a=10;
a = a+b;``````

The scope of first block is through out main function block.

And the scope of second block is with in second block.

What does this mean π€?

Means variable a & b from first block are accessible to whole main function block.

And declared variable a from second block is only accessible in 2nd block.

Some are thinking why there is no any error of declaring variable a twice…π€

We declared variable a in two different blocks. Every block has its own copy of variables in memory.

First of all we go through execution of program.

• Main function is called.
• int a and b got memory and scope for a & b is through out main function.
• Then we enter into second block.
• The first statement of second block is a=a+b
• but a and b are not found in second block (remember we have not executed below statements )
• So a & b found is upper block i.e. in first block.
• now value of a is 3.
• Next statement is int a=10;
• Now a got memory and scope of this a is only for second block.
• Next statement is a=a+b
• Now a is already declared in 2nd block. (ex. you have 2 rooms and you are in 2nd room. You want water bottle. Water bottle is in both rooms. But its already in your room so why you’re looking for first room.)
• Therefore a=a+b becomes 10+2 which is 12 and assigned to second block variable a
• Then we again come into first block from second block.
• Now what is value of a in first block its 3. We can’t access value of ‘a’ from second block because its scope is only in second block.

That’s why output is 3.

If you have any problems mention in the comment below….

Thank You……

## 2) How many objects are created hereβ¦?

Depends upon, whether “abc” string literal is already present in String Constant Pool (SCP) or not.

If “abc” already present in SCP, then only one string object will be created in heap and s will be referring to that object.

In case “abc” is not present in SCP, then two objects will be created, one in heap which will be referred by s, and second one in SCP, having an internal reference variable.

So, options are both 1 and 2, its a multiple choice question.

Two :- If string abc is not there in string literal pool.

One:- If string is already in string literal pool

## 3) Question

Before going to answer, I have a question.

What printf() returns π€ ?

Every function returns something it may be int, float, char, string,….

So what printf() returns?

printf() returns an integer value.

The printf() function is used for printing the output. It returns the number of characters that are printed. If there is some error then it returns a negative value.

1. First outer printf() starts executing, but inside printf() there is another printf(). So now inner printf() is executing .
2. Inner printf() contains printf(“%d\n”,1) means it will print 1 and then return value 2. Whyπ€? 1 and \n are printed. \n is also a character. That’s why inner printf returns 2.
3. Now outer printf looks like this(After completely executing inner printf) printf(“%d”, 2 + 1) (Here 2 is returned value from inner printf)
4. Now 3 will be printed and outer printf returns 1 but we are not assigning this 1 to any other variable.

That’s why output is

``````1
3``````

If you have any questions, ask below in comment section..

## 4) Question

Before going to answer let me clear one thing.

What is return type of scanf()? and What scanf() returns?

In C, a scanf function returns the number of successful inputs scanned and assigned. It returns zero in case of matching failure occurs and EOF is returned if the input is reached the end and no data is read successfully.

In above code,

``````1
2
``````

inputs are provided, means 1 will get assigned to a and 2 will get assigned to b.

But we know, what scanf returns.

It returns number of successful inputs . Here a & b are integer & input provided is also integers. So scanf returns the value 2 and value 2 will get assigned to variable a.

So now What is the value of a?

Now value of a becomes 2.

That’s why output is 2.

If you have any doubts, ask below in comment section..

## 5) Question

Comma works as an operator here.

Precedence of comma operator is least in operator precedence table.

So the assignment operator takes precedence over comma and the expression βa = 1, 2, 3β becomes equivalent to β(a = 1), 2, 3β.

That is why we get output as 1 in the second program.

``````

#include<stdio.h>

int main(void)
{
int a;
a = (1, 2, 3);
printf("%d", a);
return 0;
}``````

Now guess the output?

3? Yes you are right.

In above program, brackets are used so comma operator is executed first and we get the output as 3 .

If you have any doubts, mention below in comment section….