Nothing Special   »   [go: up one dir, main page]

Basics, Operators, I/O Functions,: Conditional and Looping Statement Basics 1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Basics, Operators, I/O functions, Conditional and Looping statement

Basics
1.
Which of the following special symbol allowed in a variable name?
A.* (asterisk) B.| (pipeline)
C.- (hyphen) D._ (underscore)

Answer: Option D

2.

What will be the output of the program?


#include<stdio.h>
int main()
{
unsigned int i = 65536; /* Assume 2 byte integer*/
while(i != 0)
printf("%d",++i);
printf("\n");
return 0;
}
A.Infinite loop
B.0 1 2 ... 65535
C.0 1 2 ... 32767 - 32766 -32765 -1 0
D.No output
Answer & Explanation

Answer: Option D

Explanation:

Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535.

Step 1:unsigned int i = 65536; here variable i becomes '0'(zero). because unsigned int varies
from 0 to 65535.

Step 2: while(i != 0) this statement becomes while(0 != 0). Hence the while(FALSE)
condition is not satisfied. So, the inside the statements of while loop will not get executed.

Hence there is no output.

Note: Don't forget that the size of int should be 2 bytes. If you run the above program in GCC
it may run infinite loop, because in Linux platform the size of the integer is 4 bytes.
3.

How would you round off a value from 1.66 to 2.0?


A.ceil(1.66) B.floor(1.66)
C.roundup(1.66) D.roundto(1.66)
Answer & Explanation

Answer: Option A

Explanation:

/* Example for ceil() and floor() functions: */

#include<stdio.h>
#include<math.h>

int main()
{
printf("\n Result : %f" , ceil(1.44) );
printf("\n Result : %f" , ceil(1.66) );

printf("\n Result : %f" , floor(1.44) );


printf("\n Result : %f" , floor(1.66) );

return 0;
}
// Output:
// Result : 2.000000
// Result : 2.000000
// Result : 1.000000
// Result : 1.000000

4.

What will be the output of the program?


#include<stdio.h>
int main()
{
float a = 0.7;
if(0.7 > a)
printf("Hi\n");
else
printf("Hello\n");
return 0;
}
A.Hi B.Hello
C.Hi Hello D.None of above
Answer & Explanation

Answer: Option A
Explanation:

if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is
greater than the float variable a. Hence the if condition is satisfied and it prints 'Hi'
Example:

#include<stdio.h>
int main()
{
float a=0.7;
printf("%.10f %.10f\n",0.7, a);
return 0;
}
Output:
0.7000000000 0.6999999881

2)
What will be output of the following program?

#include<stdio.h>
int main(){
int i=5,j;
j=++i+++i+++i;
printf("%d %d",i,j);
return 0;
}

Explanation

Compiler will treat this expression j = ++i+++i+++i; as


i = ++i + ++i + ++i;

Initial value of i = 5 due to three pre increment operator final


value of i=8.
Now final value of i i.e. 8 will assigned to each variable as shown
in the following figure:

j=8+8+8
j=24 and
i=8
What will be output of the following program?

#include<stdio.h>
int main(){
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
i = 2 + 2 * 1
i = 4
Now i will be incremented by one so i = 4 + 1 = 5

(5)
What will be output of the following program?

#include<stdio.h>
void main(){
int x;
x=10,20,30;
printf("%d",x);
return 0;
}

Explanation

Output: 10

Explanation :
Precedence table:

Operator Precedence Associative


= More than , Right to left
, Least Left to right

Since assignment operator (=) has more precedence than comma


operator .So = operator will be evaluated first than comma operator.
In the following expression
x = 10, 20, 30
First 10 will be assigned to x then comma operator will be
evaluated.

6)What will be output of the following program?

#include<stdio.h>
int main(){
int a=0,b=10;
if(a=0){
printf("true");
}
else{
printf("false");
}
return 0;
}

Explanation
Output: false

(7)
What will be output of the following program?

#include<stdio.h>
int main(){
int a;
a=015 + 0x71 +5;
printf("%d",a);
return 0;
}

Explanation

Output: 131

(8)What will be output of the following program?

#include<stdio.h>
int main(){
printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
return 0;
}

Explanation

Output:

Turbo C++ 3.0: 8 4 10


Explanation:
3.14f is floating point constant. Its size is 4 byte. 3.14 is double constant (default). Its size is 8
byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size
of data type which is written inside the(). It is keyword.
(9)What will be output of the following program?

#include<stdio.h>
int main(){
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}

Explanation

Output:

Turbo C++ 3.0: 5 20 100


By default x, y, z are auto type data which are stored in stack in memory. Stack is LIFO data
structure. So in stack first stores 100 then 20 then 5 and program counter will point top stack
i.e. 5. Default value of %d in printf is data which is present in stack. So output is revere order
of declaration. So output will be 5 20 100.

(11)What will be output of the following program?

#include<stdio.h>
int main(){
int a;
a=sizeof(!5.6);
printf("%d",a);
return 0;
}

Explanation

Output:: 2
Explanation:
! is negation operator it return either integer 0 or 1.
! Any operand = 0 if operand is non zero.
! Any operand = 1 if operand is zero.
So, !5.6 = 0
Since 0 is integer number and size of integer data type is two byte.

(12)What will be output of the following program?

#include<stdio.h>
int main(){
float a;
(int)a= 45;
printf("%d,a);
return 0;
}

Explanation

Output:
Turbo C++ 3.0: Compilation error
Explanation:
After performing any operation on operand it always return some constant value.

(int) i.e. type casting operator is not exception for this. (int) a will return one constant value
and we cannot assign any constant value to another constant value in c.

(int)a = 45; is equivalent to


3456 = 45 ( Here 3456 in any garbage value of int(a))

You might also like