1. State whether the following are valid statements. If they are not. Assume var
ID: 3793109 • Letter: 1
Question
1. State whether the following are valid statements. If they are not. Assume variables, a, b & c are declared as integer variables and have been given values (a = 5, b = 10, c = 12) prior to each of the following statements. If they are valid explain what the statement will do. (3 points each)
a) if (a = (b == c)) a = 1; else a = -1;
b) a += b ++;
c) (a - b) = c;
d) a = (b - 1) ? b : c;
e) a += b == c;
f) a-=1;
g) a=-1;
h) b ** b;
i) int x [a];
j) a = —a - -;
k) unsigned char m = 5;
l) a * = ++ b;
m) b++ = a;
n) a += b++ + ++c;
o) char N [ ];
p) char L [ ] — *abcd”;
2. What will the following section of code print? (5 pts)
for (int i = 0; i < 15; i +=3)
cout << i++ << endl;
3. What will the following section of code print? (5 pts)
int i;
for (i = 0; i < 10; i++)
if (i%3)
cost <<“Got a line with i = “ <<i<<endl;
4. What does the following section of code print? (5 pts)
int a;
int b;
int c;
a = 11
b = 7
c = (a-1.0)/ (b - 4) / 0.7;
cost << “Results is “ <<c<<endl;
5. What will the following section of code print? (5 pts)
int j (0);
int k [5];
int m (0);
while (j< 5)
k [j] = j++ * 2;
for (j = 0; j <5; j++)
m += k [j];
cost << “Answer is “ << m << endl;
6. Write a loop to print all integers that are multiples of eleven between 1 and 1000. (5 pts)
7. Write a loop which will: (10 pts)
Read a character from the keyboard.
Print a message telling if the character is a digit, uppercase, or lowercase.
If the character is a lowercase x the loop will terminate, otherwise it reads another character.
8. Write the declaration for a variable called string holding a string up to 25 alphabetic characters (2 pts)
9. Write a piece of code using only one switch and no other control structures to read one character and do the following: (10 pts)
if a 1 print “Got a ONE”
if a 3 print “Got a THREE”
if a 5 print “Got a FIVE”
if a 7 print “Got a SEVEN”
if a 9 print “Got a NINE”
if an even number print “Got an even number”
if a non-digit print “Got a non-digit”
10. Write a section of code that will take an integer variable Y and if Y is negative, will set to zero. If Y is greater than zero, the code will add one to Y if and only if Y is also an even number (5 pts)
Explanation / Answer
HI, I have answered all parts of first 3 questions.
Please repost rest questions.
Please let me know in case of any issue in first 3 question.
1)
a = 5, b = 10, c = 12
a) if (a = (b == c))
a = 1;
else a = -1;
Valid. a = -1 [this : a = (b == c) assigns 0 in a, so if will not execute]
b) a += b ++;
Valid. a = a + b++
= 5 + 10 = 15, b = 11
c) (a - b) = c;
Invalid, wrong assignment
d) a = (b - 1) ? b : c;
Valid
b-1= 9 that is true, so it assigns b value in a
a = 9, b = 10, c = 12
e) a += b == c;
Valid
b==c => 0 (false)
so, a = a + 0 = 5, b= 10, c = 12
f) a-=1;
Valid
a = a - 1 = 4
g) a=-1;
Valid
a = -1
h) b ** b;
Invalid
(**) is not a valid operator in C++
i) int x[a];
Invalid
'a' should be consant value
j) a = —a--;
Invalid
multiple unsequenced modifications to 'a'
k) unsigned char m = 5;
Valid
l) a *= ++ b;
Valid
a = 55, b = 11
m) b++ = a;
Invalid
expression is not assignable
n) a += b++ + ++c;
Valid
a = 28, b= 11, c=
13
o) char N [ ];
Invalid
size should be mentioned while array declaration
p) char L [ ] = "abcd";
Valid
2. What will the following section of code print? (5 pts)
for (int i = 0; i < 15; i +=3)
cout << i++ << endl;
0
4
8
12
3. What will the following section of code print? (5 pts)
int i;
for (i = 0; i < 10; i++)
if (i%3)
cout <<“Got a line with i = “ <<i<<endl;
Got a line with i = 1
Got a line with i = 2
Got a line with i = 4
Got a line with i = 5
Got a line with i = 7
Got a line with i = 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.