Sunday 16 October 2011

Chapter 1: [A] Which of the following are invalid variable names and why?



BASICSALARY    _basic   basic-hra
#MEAN      group.   422
population in 2006      over time    mindovermatter
FLOAT      hELLO   queue.
team’svictory    Plot # 3   2015_DDay


BASICSALARY               Valid

_basic                             Valid

basic-hra                         ( - not allowed)

#MEAN                           ( # not allowed)

group.                             (  . not allowed)

422                                 ( only digits not allowed)

population in 2006          ( spaces not allowed)
 
over time                        ( spaces not allowed)

mindovermatter             Valid    

queue.                            (  . not allowed)

team’svictory                  (   '  not allowed)

Plot # 3                           ( spaces and # not allowed)

2015_DDay                    ( starting with digits not allowed)


Chapter 1: [B] Point out the errors, if any, in the following C statements:

(a)  int = 314.562 * 150 ;

C keyword is used as variable name. 



(b)  name = ‘Ajay’ ;

Char can only store 1 character.Also only single sided single quotes are used to enclose char.



(c)  varchar = ‘3’ ;

only single sided single quotes are used to enclose char.

varchar = '3' ; is correct



(d)  3.14 * r * r * h = vol_of_cyl ;

One and only one variable can be at the left side of an expression.



(e)  k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ;

It is assumed that arithmetic operators (*) are already present. When they are not



(f)  m_inst = rate of interest * amount in rs ;

No spaces are allowed in c variable names.



(g)  si = principal * rateofinterest * numberofyears / 100 ;

No error



(h)  area = 3.14 * r ** 2 ;

(*) is repeated        

area = 3.14 * r * 2 ;is corect



(i)  volume = 3.14 * r ^ 2 * h ;

There is no exponential operator in C.



(j)  k = ( (a * b ) + c ) ( 2.5 * a + b ) ;

(*) is missing

k = ( (a * b ) + c ) *( 2.5 * a + b ) ; is correct.


(k)  a = b = 3 = 4 ;

A single variable can not store more than one values.



(l)  count = count + 1 ;

No error



(m)  date = '2 Mar 04' ;

Char can only hold a single character.

Chapter 1: [C] Evaluate the following expressions and show their hierarchy.

(a)  g = big / 2 + big * 4 / big - big + abc / 3 ;
(abc = 2.5, big = 2, assume g to be a float)


2 / 2 + 2 * 4 / 2 - 2 + 2.5 / 3;  

1 + 2 * 4 / 2 - 2 + 2.5 / 3;

1 + 8 / 2 - 2 + 2.5 / 3;

1 + 4 - 2 + 2.5 / 3;

1 + 4 - 2 + 0; 

 5 - 2 + 0;

3+ 0

3
so
g = 3


(b)  on = ink * act / 2 + 3 / 2 * act + 2 + tig ;
(ink = 4, act = 1, tig = 3.2, assume on to be an int)

4 * 1 / 2 + 3 / 2 * 1  + 2 + 3.2

4 / 2 + 3 / 2 * 1  + 2 + 3.2

2 + 3 / 2 * 1  + 2 + 3.2

2 + 1 * 1  + 2 + 3.2 

2 + 1  + 2 + 3.2

3 + 2 + 3.2

5  + 3.2
  
8
so
on = 8


(c)  s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ;
(qui = 4, add = 2, god = 2, assume s to be an int)

4 * 2 / 4 - 6 / 2 + 2 / 3 * 6 / 2 

8 / 4 - 6 / 2 + 2 / 3 * 6 / 2 

2 - 6 / 2 + 2 / 3 * 6 / 2 

2  - 3 + 2 / 3 * 6 / 2 

2 - 3 + 0 * 6 / 2 

2 - 3 + 0 / 2

2 - 3 + 0 
-1 + 0

-1 

so
s= -1

 
(d)  s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g ;
(a = 4, g = 3, assume s to be an int)

1 / 3 * 4 / 4 - 6 / 2 + 2 / 3 * 6 / 3

0 * 4 / 4 - 6 / 2 + 2 / 3 * 6 / 3 

0 / 4 - 6 / 2 + 2 / 3 * 6 / 3 

0 - 6 / 2 + 2 / 3 * 6 / 3 

0 - 3 + 2 / 3 * 6 / 3

0 - 3 + 0 * 6 / 3

0 - 3 + 0 / 2
  
0 - 3 + 0

-3 +0

-3 

so 
s = -3



Chapter 1: [E] Convert the following equations into corresponding C statements.









 
corresponding C  statement:
Z = ( 8.8 * ( a + b ) * 2 / c - 0.5 +2 * a / ( q + r ) ) / ( ( a + b ) * ( 1 / m ) )








corresponding C  statement:
X = (-b + ( b * b ) + 2 * (4*a*c) ) / ( 2*a )








corresponding C  statement:


R =( 2 * v + 6.22 * ( c + d ) ) / ( ( g + v ) )










corresponding C  statement:
A = ( 7.7 * b * ( x * y + a ) / c - 0.8 ) / ( ( x + a ) * ( 1 / y ) )



Chapter 1: [F] What would be the output of the following programs:

(a)

main( )
{

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 ) ;
}

Output
0 2 0.000000 2.000000



(b)
 
main( )
{
int  a, b ;
a = -3 - - 3 ;
b = -3 - - ( - 3 ) ;
printf ( "a = %d b = %d", a, b ) ;
}

Output
a = 0 b = -6


(c)
main( )
{
float a = 5,  b = 2 ;
int c ;
c = a % b ;
printf ( "%d", c ) ;
}

Output
modulus operator can not be used between floats.
Compiler will give the error and there will be no output.



(d)

main( )
{
printf ( "nn \n\n nn\n" ) ;
printf ( "nn /n/n nn/n" ) ;
}


Output
nn

 nn
nn /n/n nn/n





(e) 

main( ) 
int a, b ; 
printf ( "Enter values of a and b" ) ; 
scanf ( " %d %d ", &a, &b ) ; 
printf ( "a = %d b = %d", a, b ) ; 
 

Output

Enter values of a and b
1
2
a = 1 b = 2 





(f) 
main( ) 
int p, q ; 
printf ( "Enter values of p and q" ) ; 
scanf ( " %d %d ", &p, &q ) ; 
printf ( "p = %d q =%d", p, q ) ; 


Output

Enter values of p and q
5 10
p=5 q=10













Chapter 1: [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 





(h)  The maximum value that an integer constant can have is

(1)  -32767
(2)  32767
(3)  1.7014e+38
(4)  –1.7014e+38




(i)  A C variable cannot start with

(1)  An alphabet
(2)  A number
(3)  A special symbol other than underscore
(4)  Both (2) & (3) above






(j)  Which of the following statement is wrong

(1)  mes = 123.56 ;
(2)  con = 'T' * 'A' ;
(3)  this = 'T' * 20 ;
(4)  3 + a = b ;





(k)  Which of the following shows the correct hierarchy of
arithmetic operators in C

(1)  **, * or /, + or -
(2)  **, *, /, +, -
(3)  **, /, *, +, -
(4)  / or *, - or +




(l)  In b = 6.6 / a + 2 * n ; which operation will be performed
first?

(1)  6.6 / a
(2)  a + 2
(3)  2 * n
(4)  Depends upon compiler




(m)  Which of the following is allowed in a C Arithmetic
instruction

(1)  [ ]
(2)  { }
(3)  ( )
(4)  None of the above






(n)  Which of the following statements is false

(1)  Each new C instruction has to be written on a separate
line
(2)  Usually all C statements are entered in small case letters 
(3)  Blank spaces may be inserted between two words in a C
statement
(4)  Blank spaces cannot be inserted within a variable name






(o)  If a is an integer variable, a = 5 / 2 ; will return a value

(1)  2.5
(2)  3
(3)  2
(4)  0





(p)  The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to

(1)  8.28
(2)  6.28
(3)  3.14
(4)  0





(q)  The expression, a = 30 * 1000 + 2768 ; evaluates to

(1)  32768
(2)  -32768
(3)  113040
(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)  Atleast 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

Saturday 15 October 2011

Chapter 1: [H] Write C programs for the following:

(a) Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.



/* This program takes basic salary from user, calculates dearness and house rent
allowances and then gross salary and shows results.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*declaring veriables*/
    float bsalary, dallow, hrallow, grsalary;
   
    /*prompt to take bsalary as input*/
    printf("Please enter the basic salery: ");
   
    /*taking input*/
    scanf("%f",&bsalary);
   
    /*calculations*/
    dallow = 0.4* bsalary;
    hrallow = 0.2 * bsalary;
    grsalary = bsalary + dallow + hrallow;
  
    /*showing results*/
    printf("\n\nBasic Salary          :          %f Rs\n",bsalary);
    printf("\nDearness Allownce     :          %f Rs \n",dallow);
    printf("\nHouse Rant Allownce   :          %f Rs \n",hrallow);
    printf("___________________________________________\n");
    printf("\nGross Salary          :          %f Rs\n",grsalary);
    getche();
    return 0;
}


(b) The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.


/* This program takes lenght in kilo-meters from user, converts it into meters,
centimeters, feet and inches and then shows results .*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*delaring and initializing variables*/
    float km=0, meter=0, cm=0, feet=0, inch=0;
   
    /*prompt for input*/
    printf("Please enter the distance in 'kilo meters': ");
  
    /*taking length in kilo-meters as input*/
    scanf("%f",&km);
   
    /*calculations*/
    meter = km * 1000;
    cm = meter * 100;
    feet = meter * 3.280839895;
    inch = feet * 12;
   
    /*showing results*/
    printf("Here is the conversion of %f km",km);
    printf("\n\n      In Meters: %f m      \n\n      In Centi Meters: %f cm      \n\n      In Feet: %f feet      \n\n      In inches: %f inch     ",meter,cm,feet,inch);    

    getche();
    return 0;
   
}


(c) If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.



/* This program takes marks of a student in five subjects from user,
finds aggregate marks and percentage, and then shows results.*/
#include<stdio.h>
#include<conio.h>
int main ()
{
    /*Declaring and initializing veriables*/
    int sub1 = 0, sub2 = 0, sub3 = 0, sub4 = 0, sub5 = 0, aggmarks  = 0;
    float percentmarks = 0;
   
    /*Taking marks as input from the user*/
    printf("Plese enter the marks of the student in subject 1: ");
    scanf("%d",&sub1);
    printf("\nnow enter the marks of the student in subject 2: ");
    scanf("%d",&sub2);
    printf("\nnow enter the marks of the student in subject 3: ");
    scanf("%d",&sub3);
    printf("\nnow enter the marks of the student in subject 4: ");
    scanf("%d",&sub4);
    printf("\nnow enter the marks of the student in subject 5: ");
    scanf("%d",&sub5);
   
    /*Calculations*/
    aggmarks = sub1 + sub2 +sub3 +sub4 +sub5;
    percentmarks = (aggmarks/500.0)*100.0;
  
    /*Showing Results*/
    printf("\n________________________________________\n");
    printf("\nAggregate Marks of the Student are: %d \n",aggmarks);
    printf("\nwhich are %f percent of total marks.",percentmarks);
    getche();
    return 0;
}