Let Us C++ Solution
Getting Started
[A] Which of the following are invalid variable names and why?
BASICSALARY (valid)
_basic (valid)
basic-hra (invalid , we cannot use hyphen in variable name)
#MEAN (invalid , variable name must begin with an alphabet or underscore)
group (invalid A is not allowed in variable name)
422 (invalid A variable name cannot begin with a digit)
population in 2012 (invalid variable name cannot contain spaces)
over time (invalid variable name cannot contain spaces)
mindovermatter (valid)
FLOAT (valid)
Hello (valid)
queue (invalid A is not allowed in variable name)
team'svictory (invalid A ',, is not allowed in variable name)
Plot # 3 (invalid, The '#' and spaces are not allowed in variable name)
2012_DDay (invalid, A variable name must with an alphabet or an underscore)
[B] Point out the errors, if any, in the following C statements:
(a) int = 314.562*150;
Error. int is a keyword hence should not be used as a variable.
(b) name = 'Ali'
Error. 'Ali' is an invalid character constant.
(c) varchar = '3'
No error
(d) 3.14*r*r*h = vol_of_cyl;
Error. on the left-hand side of equal to (=) there can only be a variable.
(e) k = (a * b) ( c + (2.5a + b) (d + e) ;
Error. Multiplication operator (*) missing among the three expressions and also between 2.5 and variable a.
(f) m_inst = rate of interest * amount in rs;
Error. rate of interest and amount in rs are invalid variable names.
(g) si = principal * rateofinterest * numberofyear / 100;
No error.
(h) area = 3.14 * r ** 2;
Error. '**' is an invalid operator.
(i) volume = 3.14 * r ∧ 2 * h;
Error. The expression 3.14 * r will result in a float. the '∧ ' operator cannot be used with floats.
(j) k = ((a * b) + c) ( 2.5 * a + b);
Error. Multiplication operator ( * ) missing between the two expressions given in parentheses.
(k) a = b = 3 = 4;
Error. A constant 3 cannot be on the left hand side of the assignment operator.
(l) count = count + 1;
No error.
(m) date = '2 Mar 04;
Error. Double quotes ("") should be used instead of ('') to enclose the string 2 Mar 04.
[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)
Answer
g = 2 / 2 + 2 * 4 / 2 - 2 + 2.5 / 3 operation: /
g = 1 + 2 * 4 / 2 - 2 + 2.5 / 3 operation: *
g = 1 + 8 / 2 - 2 + 2.5 / 3 operation: /
g = 1 + 4 - 2 + 2.5 / 3 operation: /
g = 1 + 4 - 2 + 0.83 operation: +
g = 5 - 2 + 0.83 operation: -
g = 3 + 0.83 operation: +
g = 3.83
(b) on = ink * act / 2 + 3 / 2 * act + 2 + tig ;
(ink = 4, act = 1, tig = 3.2, assume on to be an int)
Answer
on = 4 * 1 / 2 + 3 / 2 * 1 + 2 + 3.2 operation: *
on = 4 / 2 + 3 / 2 * 1 + 2 + 3.2 operation: /
on = 2 + 3 / 2 * 1 + 2 + 3.2 operation: /
on = 2 + 1 * 1 + 2 + 3.2 operation: *
on = 2 + 1 + 2 + 3.2 operation: +
on = 3 + 2 + 3.2 operation: +
on = 5 + 3.2 operation: +
on = 8
No comments:
Post a Comment