for each section question give examples please / exam will have three kinds of q
ID: 3589548 • Letter: F
Question
for each section question give examples please / exam will have three kinds of questions (in sections)
// for all questions, you can assume that the code that we have been writing to start our labs is already
// in place
// for the first section, I will give you some variables with values such as
int X = 5;
int Y = 10;
int Z = 12;
// then you will have a series of C++ statements where you must tell me
// Is the statement valid or not
// if valid, tell me what the new values of the variables used will be
// if not valid, tell me why it is not valid
// changes in one question will not affect the variables in any other question
// all questions here will have the same value, there will be 15 to 20 of this type of question
X *= ++Y; // valid, Y is now 11, X is now 55
X++ = Y; // not valid, cannot do math on left side of =
X + 1 = Y; // not valid, cannot do math on left side of =
X = Y + (Z = 9); // valid, Z is 9, X is 19
int A [X]; // not valid, what is inside the [ ] must be a constant
X = X++ + ++X; // valid, X is now 13
X += Y == Z; // valid, nothing changes
X = (Y > Z) ? 99 : -99; // valid, X is now -99
// for the second section you will be given pieces of code and asked to show what the code would display
// the code will be valid
// there will be three or four of this type of question
// I will show what each question will be worth
int i;
for (i = 0; i < 10; i++)
cout << i++ << endl;
// 0
// 2
// 4
// 6
// 8
int a;
int b;
int c;
a = 12;
b = 5;
c = ((a + 1.0) / b) + 0.5;
cout << "Result is " << c << endl;
// Result is 3
a = 11;
b = 5;
c = ((a + 1.0) / b) + 0.5;
cout << "Result is " << c << endl;
// Result is 2
// for the third type of question, I will ask you to write a piece of code to do some operations
// there will be three or four of this type of question
// you can assume that the code we start each lab with will already be in place
// if I ask to write a loop, you can use whichever type of loop you wish
// I may ask you to write a switch in particular to make sure you can do a switch
// write a loop to display all multiples of 5 from 1 thru 100
int i;
for (i = 5; i <= 100; i += 5)
cout << i << endl;
for (i = 1; i <= 100; i++)
if ((i % 5) == 0)
cout << i << endl;
else;
// given that you have an existing integer variable A, then if A is greater than 1 make it zero, if it is
// less than zero, make it -1. If it is zero, make it 100
if (A > 1)
A = 0;
else
if (A < 0)
A = -1;
else
if (A == 0)
A = 100;
else;
Explanation / Answer
Read the commented part careful. And try to analyze on why each step happened. That will give you a clear understanding, and then, you can apply the logic on any values.
// for all questions, you can assume that the code that we have been writing to start our labs is already
// in place
// for the first section, I will give you some variables with values such as
int X = 5;
int Y = 10;
int Z = 12;
// then you will have a series of C++ statements where you must tell me
// Is the statement valid or not
// if valid, tell me what the new values of the variables used will be
// if not valid, tell me why it is not valid
// changes in one question will not affect the variables in any other question
// all questions here will have the same value, there will be 15 to 20 of this type of question
X *= ++Y; // valid, Y is now 11, X is now 55
X *= ++Y;
//This can be subdivided as:
//Y = Y + 1; //Y = 11.
//X = X * Y; //X = 55.
X++ = Y; // not valid, cannot do math on left side of =
//X++ = Y;
//Invalid. The expressions are not allowed on left side of assignment.
X + 1 = Y; // not valid, cannot do math on left side of =
//This is prettymuch same like the above problem.
X = Y + (Z = 9); // valid, Z is 9, X is 19
X = Y + (Z = 9);
//The statement can be broken as:
//Z = 9;
//X = Y + Z; //So, X = 19, and Z = 9.
int A [X]; // not valid, what is inside the [ ] must be a constant
//You can declare variable sized arrays, but you're not allowed to initialize during declaration.
//This will create an integer array A of size 5.
//And no variables will be modified.
X = X++ + ++X; // valid, X is now 13
//The statement can be broken as:
//X = X + 1; //The pre-increment part. So, X = 6.
//X = X(5) + X(6);//Note that the post-increment value of X is buffered/stacked for the expression to evaluate. So, X value is now. 11.
//X = X + 1; //So, X = 12.
X += Y == Z; // valid, nothing changes
//Y == Z evaluates to false, and the integer value is 0.
//X += 0; //Makes X value be intact.
X = (Y > Z) ? 99 : -99; // valid, X is now -99
//This is a ternary operator. If Y > Z, Assign 99 to X, else assign -99 to X.
//As Y(10) is not greater than Z(12), the false block i.e., -99 is assigned to X.
//So, X value is now -99.
// for the second section you will be given pieces of code and asked to show what the code would display
// the code will be valid
// there will be three or four of this type of question
// I will show what each question will be worth
int i;
for (i = 0; i < 10; i++) //This loop runs for values i = 0 upto 9.
cout << i++ << endl; //i value is printed, and then incremented.
//So, Yes, the output is as given by you.
// 0
// 2
// 4
// 6
// 8
int a;
int b;
int c;
a = 12;
b = 5;
c = ((a + 1.0) / b) + 0.5;
//c = ((12 + 1.0) / 5) + 0.5
// = (13.0 / 5) + 0.5
// = 2.6 + 0.5
// = 3.1
// And finally, as c is an integer variable. only integral part will be stored, i.e., c = 3.
cout << "Result is " << c << endl;
// Result is 3
a = 11;
b = 5;
c = ((a + 1.0) / b) + 0.5;
//c = ((11 + 1.0) / 5) + 0.5
// = (12.0 / 5) + 0.5
// = 2.4 + 0.5
// = 2.9
// And finally, as c is an integer variable. only integral part will be stored, i.e., c = 2
cout << "Result is " << c << endl;
// Result is 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.