Page 1 of 5
Assignment 04 Programming in C 1090
[A] What would be the output of the following programs?
(a)
main( )
{
int a = 300, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ; } Garbage Value 200
(b)
main( )
{
int a = 500, b, c ;
if ( a >= 400 )
b = 300 ;
c = 200 ;
printf ( "\n%d %d", b, c ) ; } 300 200
(c)
main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;} Blank Screen
(d)
main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ; 3
printf ( "\n%d", y ) ; } 5
(e)
main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;} x and y are equal
[B] What would be the output of the following programs?
(a)
main( ){
int i = 4, z = 12 ;
if ( i = 5 || z > 50 )
printf ( "\nDean of students affairs" ) ;
else
printf ( "\nDosa" ) ;} Dosa
Page 2 of 5
(b)
main( )
{
int i = 4, z = 12 ;
Chapter 2: The Decision Control Structure 85
if ( i = 5 && z > 5 )
printf ( "\nLet us C" ) ;
else
printf ( "\nWish C was free !" ) ;} Wish C was free
(c)
main( )
{
int i = 4, j = -1, k = 0, w, x, y, z ;
w = i || j || k ;
x = i && j && k ;
y = i || j && k ; w = 1 x = 0 y = 1 z = 1
z = i && j || k ;
printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ;}
(d)
main( )
{
int i = 4, j = -1, k = 0, y, z ;
y = i + 5 && j + 1 || k + 2 ;
z = i + 5 || j + 1 && k + 2 ;
printf ( "\ny = %d z = %d", y, z ) ;} y = 1 z= 1
(e)
main( )
{
int i = -3, j = 3 ;
if ( !i + !j * 1 )
printf ( "\nMassaro" ) ;
else
printf ( "\nBennarivo" ) ;} Bennarivo
[C] What would be the output of the following programs?
(a)
main( ){
int i = -4, j, num ;
j = ( num < 0 ? 0 : num * num ) ;
printf ( "\n%d", j ) ;} Garbage Value
(b)
main( ){
int k, num = 30 ;
k = ( num > 5 ? ( num <= 10 ? 100 : 200 ) : 500 ) ;
printf ( "\n%d", num ) ;} 30
(c)
main( ) { Welcome
int j = 4 ;
( !j != 1 ? printf ( "\nWelcome") : printf ( "\nGood Bye") ) ;}
Page 3 of 5
[D] What would be the output of the following programs?
(a)
main( )
{
int j ; No Output
while ( j <= 10 ) because j not initialized with a value
{
printf ( "\n%d", j ) ;
j = j + 1 ;}}
(b)
main( )
{
int i = 1 ;
while ( i <= 10 ) ; No output
{ because ; given after while
printf ( "\n%d", i ) ;
i++ ;}}
(c)
main( )
{
int j ;
while ( j <= 10 ) No Output
{ because j not initialized with a value
printf ( "\n%d", j ) ;
j = j + 1 ;}}
(d)
main( )
{
int x = 1 ;
while ( x == 1 ) 0
{
x = x - 1 ;
printf ( "\n%d", x ) ;}}
(e)
main( )
{
int x = 1 ;
while ( x == 1 )
x = x - 1 ; 0
printf ( "\n%d", x ) ;}
[E] What would be the output of the following programs?
(a)
main( )
{
char suite = 3 ;
switch ( suite )
{
case 1 :
printf ( "\nDiamond" ) ; Heart
case 2 : I thought one wears a suite
printf ( "\nSpade" ) ;
Page 4 of 5
default :
printf ( "\nHeart") ;
}
printf ( "\nI thought one wears a suite" ) ;}
(b)
main( )
{
int c = 3 ;
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
case 12 :
printf ( "I am in case 12 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;}} I am in case 3
(c)
main( )
{
int k, j = 2 ;
switch ( k = j + 1 )
{
case 0 :
printf ( "\nTailor") ;
case 1 :
printf ( "\nTutor") ;
case 2 :
printf ( "\nTramp") ;
default :
printf ( "\nPure Simple Egghead!" ) ;}} Pure Simple Egghead
[F] What would be the output of the following programs?
(a)
main( )
{
printf ( "\nOnly stupids use C?" ) ;
display( ) ;
}
display( )
{
printf ( "\nFools too use C!" ) ;
main( ) ;} Both lines printed infinitely
(b)
main( )
{
printf ( "\nC to it that C survives" ) ; line printed infinitely
main( ) ;}
Page 5 of 5
[G] What would be the output of the following programs?
(a)
main( )
{ int num[26], temp ;
num[0] = 100 ; num[25] = 200 ;
temp = num[25] ; num[25] = num[0] ;
num[0] = temp ;
printf ( "\n%d %d", num[0], num[25] ) ; } 200 100
(b)
main( )
{ int array[26], i ;
for ( i = 0 ; i <= 25 ; i++ )
{ array[i] = 'A' + i ;
printf ( "\n%d %c", array[i], array[i] ) ; } }
program will print ASCII values of all Alphabets.
[H]State whether the following statements are true or false.
(a) All arithmetic operators have the same level of precedence. F
(b) The modulus operator % can be used only with integers. T
(c) The operators <=, >= and != all enjoy the same level of priority. T
(d) During modulo division, the sign of the result is positive, if both the
operands are of the same sign. T
(e) In C, if a data item is zero, it is considered false. T
(f) The expression !(x<=y) is same as the expression x>y. T
(g) A unary expression consists of only one operand with no operators. T
(h) Associativity is used to decide which of several different expressions
is evaluated first. T
(i) An expression statement is terminated with a period. F
(j) During the evaluation of mixed expressions, an implicit cast T
is generated automatically.
(k) An explicit cast can be used to change the expression. T
(1)Parentheses can be used to change the order of evaluation expressions. T
[I]Given the statement int a = 10, b = 20, c;
determine whether each of the following statements are true or false.
(a) The statement a = + 10, is valid. F
(b) The expression a + 4/6 * 6/2 evaluates to 11. F
(c) The expression b + 3/2 * 2/3 evaluates to 20. T
(d) The statement a + = b; gives the values 30 to a and 20 to b. T
(e) The statement ++a++; gives the value 12 to a. T
(f) The statement a = 1/b assigns the value 0.5 to a F
[J]State whether the following statements are true or false.
(a) The purpose of the header file <stdio.h> is to store the programs
created by users. F
(b) The C standard function that receives a single character from the
keyboard is getchar. T
(c) The getchar cannot be used to read a line of text from the keyboard. T
(d) The input list in a scanf statement can contain one or more variables.
(e) When an input stream contains more data items than the number of T
specifications in a scanf statement, the unused items will be used by
the next scanf call the program. T
(h) The scanf function cannot be used to read a single character from the
Keyboard F