Professional Documents
Culture Documents
Let Us C Soln PDF
Let Us C Soln PDF
CS 1090
Exercises Chapter 1
[A] Which of the following are invalid variable names and why?
BASICSALARY valid
_basic valid
basic-hra invalid
- in variable name
#MEAN group. Invalid # in starting of variable name
422 invalid all numeric characters
population in 2006
invalid space between variable name
over time
invalid space between variable name
mindovermatter valid
FLOAT invalid C keyword
hELLO valid
queue valid
teamsvictory invalid special character in variable name
Plot # 3 invalid special character # in variable name
2015_DDay
invalid beginning with number is not permitted
[B] Point out the errors, if any, in the following C statements:
{
int i = 2, j = 3, k, l ;
float a, b ;
k=i/j*j;
l=j/i*i;
a=i/j*j;
b=j/i*i;
printf( "%d %d %f %f", k, l, a, b ) ;
}
0 2
0.0000 2.0000
(b) main( )
{
int a, b ;
a = -3 - - 3 ;
b = -3 - - ( - 3 ) ;
printf ( "a = %d b = %d", a, b ) ;
}
a=0 b=-6
(c) main( )
{
int a = 5, b = 2 ;
int c ;
c=a%b;
printf ( "%d", c ) ;
}
1
(d) main( )
{
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
}
nn
nn
nn /n/n nn/
(e) main( )
{
int a, b ;
printf ( "Enter values of a and b" ) ;
scanf ( " %d %d ", &a, &b ) ;
printf ( "a = %d b = %d", a, b ) ;
}
Enter values of a and b
23
a=2 b=3
(f) main( )
{
int p, q ;
printf ( "Enter values of p and q" ) ;
scanf ( " %d %d ", &p, &q ) ;
printf ( "p = %d q =%d", p, q ) ;
}
Enter values of p and q
5 10
p=5 q=10
[G] Pick up the correct alternative for each of the following questions:
(a) C language has been developed by
(1) Ken Thompson
(2) Dennis Ritchie
(3) Peter Norton
(4) Martin Richards
(b) C can be used on
(1) Only MS-DOS operating system
(2) Only Linux operating system
(3) Only Windows operating system
(4) All the above
(c) C programs are converted into machine language with the help of
(1) An Editor
(2) A compiler
(3) An operating system
(4) None of the above
(d) The real constant in C can be expressed in which of the following forms
(1) Fractional form only
(2) Exponential form only
(3) ASCII form only
(4) Both fractional and exponential forms
(e) A character variable can at a time store
(1) 1 character
(2) 8 characters
(3) 254 characters
(4) None of the above
(f) The statement char ch = Z would store in ch
(1) The character Z
(2) ASCII value of Z
(3) Z along with the single inverted commas
(4) Both (1) and (2)
(g) Which of the following is NOT a character constant
(1) Thank You
(2) Enter values of P, N, R
(3) 23.56E-03
(4) All the above
(4) 0
(r) The expression x = 4 + 2 % - 8 evaluates to
(1) -6
(2) 6
(3) 4
(4) None of the above
(s) Hierarchy decides which operator
(1) is most important
(2) is used first
(3) is fastest
(4) operates on largest numbers
(t) An integer constant in C must have:
(1) At least one digit
(2) At least one decimal point
(3) A comma along with digits
(4) Digits separated by commas
(u) A character variable can never store more than
(1) 32 characters
(2) 8 characters
(3) 254 characters
(4) 1 character
(v) In C a variable cannot contain
(1) Blank spaces
(2) Hyphen
(3) Decimal point
(4) All the above
(w) Which of the following is FALSE in C
(1) Keywords can be used as variable names
(2) Variable names can contain a digit
(3) Variable names do not contain a blank space
(4) Capital letters can be used in variable names
(x) In C, Arithmetic instruction cannot contain
(1) variables
(2) constants
(3) variable names on right side of =
(4) constants on left side of =
(y) Which of the following shows the correct hierarchy of arithmetic operations in C
(1) / + * (2) * - / +
(3) + - / *
(4) * / + (z) What will be the value of d if d is a float after the operation d = 2 / 7.0?
(1) 0
(2) 0.2857
(3) Cannot be determined
(4) None of the above
Exercises Chapter 2
300
200
(c) main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;
}
10 20
(d) main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ;
printf ( "\n%d", y ) ;
}
3 5
(e) main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
0 50 1
(h) main( )
{
int i = 65 ;
char j = A ;
if ( i == j )
printf ( C is WOW ) ;
else
printf( "C is a headache" ) ;
}
C is WOW
(i) main( )
{
int a = 5, b, c ;
b = a = 15 ;
c = a < 15 ;
printf ( "\na = %d b = %d c = %d", a, b, c ) ;
}
1 20 1
{
float a = 12.25, b = 12.52 ;
if ( a = b )
printf ( "\na and b are equal" ) ;
}
Need comparison operator == inside if condition
(b) main( )
{
int j = 10, k = 12 ;
if ( k >= j )
{
{
k=j;
j=k;
}
}
}
No Error
(c) main( )
{
if ( 'X' < 'x' )
printf ( "\nascii value of X is smaller than that of x" ) ;
}
No Error
(d) main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "\n%d", x ) ;
}
(condition) missing
(f) main( )
{
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
printf ( "\nCarpathians" ) ;
}
(g) main( )
{
int x = 30 , y = 40 ;
if ( x == y )
printf( "x is equal to y" ) ;
elseif ( x > y )
printf( "x is greater than y" ) ;
elseif ( x < y )
printf( "x is less than y" ) ;
}
Value
a != 6 && b > 5
a == 9 || b < 3
! ( a < 10 )
! ( a > 5 && c )
5 && c != 8 || !c
a != 6 && b > 5 , 1
a == 9 || b < 3 , 0
! ( a < 10 ), 1
! ( a > 5 && c ) , 1
5 && c != 8 || !c , 1
(a) main( )
{
int i = 4, z = 12 ;
if ( i = 5 || z > 50 )
printf ( "\nDean of students affairs" ) ;
else
printf ( "\nDosa" ) ;
}
Let us C
(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 ;
z = i && j || k ;
printf ( "\nw = %d x = %d y = %d z = %d", w, x, y, z ) ;
}
y=1 z=1
(e) main( )
{
int i = -3, j = 3 ;
if ( !i + !j * 1 )
printf ( "\nMassaro" ) ;
else
printf ( "\nBennarivo" ) ;
}
Bennarivo
(f) main( )
{
int a = 40 ;
if ( a > 40 && a < 45 )
printf ( "a is greater than 40 and less than 45" ) ;
else
printf ( "%d", a ) ;
}
40
(g) main( )
{
int p = 8, q = 20 ;
if ( p == 5 && q > 5 )
printf ( "\nWhy not C" ) ;
else
printf ( "\nDefinitely C !" ) ;
}
Definitely C!
(h) main( )
{
int i = -1, j = 1, k ,l ;
k = i && j ;
l = i || j ;
printf ( "%d %d", i, j ) ;
}
1 1
(i) main( )
{
int x = 20 , y = 40 , z = 45 ;
if ( x > y && x > z )
printf( "x is big" ) ;
else if ( y > x && y > z )
printf( "y is big" ) ;
else if ( z > x && z > y )
printf( "z is big" ) ;
}
z is big
(j) main( )
{
int i = -1, j = 1, k ,l ;
k = !i && j ;
l = !i || j ;
printf ( "%d %d", i, j ) ;
}
-1 1
(k) main( )
{
int j = 4, k ;
k = !5 && j ;
printf ( "\nk = %d", k ) ;
}
k=0
[E] Point out the errors, if any, in the following programs:
(a) /* This program
/* is an example of
/* using Logical operators */
main( )
{
int i = 2, j = 5 ;
if ( i == 2 && j == 5 )
printf ( "\nSatisfied at last" ) ;
}
No Error
(b) main( )
{
int code, flag ;
if ( code == 1 & flag == 0 )
printf ( "\nThe eagle has landed" ) ;
}
int a, b ;
if ( a == 1 & b == 0 )
printf ( "\nGod is Great" ) ;
}
Missing parenthesis
(d) main( )
{
int i = 10, j = 10 ;
if ( i && j == 10)
printf ( "\nHave a nice day" ) ;
}
Have a nice day
Conditional operators
[G] 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 ) ;
}
30
(c) main( )
{
int j = 4 ;
( !j != 1 ? printf ( "\nWelcome") : printf ( "\nGood Bye") ) ;
Welcome
[H] Point out the errors, if any, in the following programs:
(a) main( )
{
int tag = 0, code = 1 ;
if ( tag == 0 )
( code > 1 ? printf ( "\nHello" ) ? printf ( "\nHi" ) ) ;
else
printf ( "\nHello Hi !!" ) ;
}
Hi
(b) main( )
{
int ji = 65 ;
printf ( "\nji >= 65 ? %d : %c", ji ) ;
}
ji >= 65 ? %d : %c
(c) main( )
{
int i = 10, j ;
i >= 5 ? ( j = 10 ) : ( j = 15 ) ;
printf ( "\n%d %d", i, j ) ;
}
10 10
(d) main( )
{
int a = 5 , b = 6 ;
( a == b ? printf( "%d",a) ) ;
}
(g) main( )
{
int x = 10, y = 20 ;
x == 20 && y != 10 ? printf( "True" ) : printf( "False" ) ;
}
False