Monday 21 November 2011

C- Programming Language


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:


(a) int = 314.562 * 150 ; variable name not present
(b) name = „Ajay ; type of variable not present
(c) varchar = „3 ; type of variable not present
(d) 3.14 * r * r * h = vol_of_cyl ; variable present at right side of expression
(e) k = ( a * b ) ( c + ( 2.5a + b ) ( d + e ) ; syntax error, missing operator
(f) m_inst = rate of interest * amount in rs ; variable names not according to convention
(g) si = principal * rateofinterest * numberofyears / 100 ; correct
(h) area = 3.14 * r ** 2 ; unknown operator **
(i) volume = 3.14 * r ^ 2 * h ; unknown operator ^
(j) k = ( (a * b ) + c ) ( 2.5 * a + b ) ; syntax error, missing operator
(k) a = b = 3 = 4 ; syntax error, dual assignment
(l) count = count + 1 ; correct
(m) date = '2 Mar 04' ; a character variable can store only one charcter









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





(1)
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 ) ;
}
0 2 0.0000 2.0000





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



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


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



(5)
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
2 3
a=2 b=3


(6)
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


(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) 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


if, if-else, Nested if-elses
[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 ) ;
}
b=garbage value, cannot be predicted
c=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 ) ;
}
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
printf ( "\nx and y are not equal" ) ;
}
x and y are equal


(f)
main( ) {
int x = 3, y, z ;
y = x = 10 ;
z = x < 10 ;
printf ( "\nx = %d y = %d z = %d", x, y, z ) ;
}
x=10 y=10 z=0


(g)
main( ) {
int k = 35 ;
printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;
}
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 ) ;
}
a=15 b=15 c=0



(j)
main( ) {
int x = 15 ;
printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ;
}
1 20 1




[B] Point out the errors, if any, in the following programs:


(a)
main( ) {
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 ) ;
}
then not a valid syntax


(e)
main( )
{
int x = 10 ;
if x >= 2
printf ( "\n%d", x ) ;
}
(condition) missing


(f)
main( )
{
int x = 10, y = 15 ;
if ( x % 2 = y % 3 )
printf ( "\nCarpathians" ) ;
}
Missing comparison operator (==)








(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" ) ;
}
else if must be separate


(h)
main( )
{
int x = 10 ;
if ( x >= 2 ) then
printf ( "\n%d", x ) ;
}
Invalid syntax then



(i)
main( )
{
int a, b ;
scanf ( "%d %d",a, b ) ;
if ( a > b ) ;
printf ( "This is a game" ) ;
else
printf ( "You have to play it" ) ;
}
invalid ; after if condition






Exercises 3



1. A constructor is called whenever
A. a object is declared
B. an object is used
C. a class is declared
D. a class is used

2. Overload function in C++
A. a group function with the same name
B. all have the same number and type of arguments

3. C++ was originally developed by
A. Nicolas Wirth
B. Donald Knuth
C. Bjarne Stroustrup
D. Ken Thompson

4. The standard C++ comment
A. /
B. // 
C. /* and */
D. None of these

5. The preprocessor directive #include is required if
A. Console output is used
B. Console input is used
C. Both console input and output is used
D. None of these


6. The operator << is called
A. an insertion operator
B. put to operator
C. either a or b
D. None of these

7. The operator >> is called
A. an extraction operator
B. a get from operator
C. either a or b
D. get to operator

8. The C++ symbol <<
A. perform the action of sending the value of expression listed as its right to the
outputs strewn as the left.
B. is used to indicate the action from right to left
C. is adopted to resemble an arrow
D. All of above

9. When a language has the capability to produce new data type, it is called
A. Extensible
B. Overloaded
C. Encapsulated
D. Reprehensible

10. C++ name was suggested by
A. Rrick Mascitti
B. Bjarne Stroustrup
C. Donald Knuth
D. Ken Thompson

11. What is a reference?
A. an operator
B. a reference is an alias for an object
C. used to rename an object
D. None of these

12. State the object oriented languages
A. C++
B. Java
C. Eiffel
D. All of the above

No comments:

Post a Comment