• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 4 out of 58 pages
Exam (elaborations)

Test Bank For C++ Programming From Problem Analysis to Program Design - 8th - 2018 All Chapters

Document preview thumbnail
Preview 4 out of 58 pages

Test Bank For C++ Programming From Problem Analysis to Program Design - 8th - 2018 All Chapters

Content preview

1. What is wrong with the following code?
j j j j j j j




int main() { i
j j j


nt x = 5; j j j


printf("The value of x is ", x); ret j j j j j j j


urn 0; j

}
Answer: The printf function is missing the format specifier for the value of x.
j j j j j j j j j j j j j j


It should be printf("The value of x is %d", x);.
j j j j j j j j j


2. What is wrong with the following code?
j j j j j j j




int main() {j j


printf("Hello,world!\n"); retur j j


n;
}
Answer: The return statement is missing a value. It should be return 0;.
j j j j j j j j j j j j


3. What is wrong with the following code?
j j j j j j j




int main() { i
j j j


nt x = 5; j j j


printf("The value of x is %d\n", y); ret j j j j j j j


urn 0; j


}
Answer: The variable y has not been declared. It should be printf("The value of
j j j j j j j j j j j j j


x is %d\n", x);.
j j j j


4. What is wrong with the following code?
j j j j j j j




int main() { i
j j j


nt x = 5; in j j j j


t y = 7; j j j


printf("The sum of %d and %d is %d\n", x, y); ret j j j j j j j j j j


urn 0; j

}
Answer: The printf function is missing the third argument, which should be t
j j j j j j j j j j j j


he sum of x and y. It should be printf("The sum of %d and %d is
j j j j j j j j j j j j j j j


%d\n", x, y,x + y);. j j j j


5. What is wrong with the following code?
j j j j j j j




int main() { i
j j j


nt x = 5; in j j j j


t y = 7; j j j


printf("The sum of %d and %d is %f\n", x, y, x + y); retu j j j j j j j j j j j j j


rn 0; j

,}
Answer: The format specifier for the sum of x and y is incorrect. Since x and y a
j j j j j j j j j j j j j j j j j


re integers, the sum will be an integer as well, so the format specifier should be
j j j j j j j j j j j j j j j j


%d instead of %f. j j j


6. What is wrong with the following code?
j j j j j j j




int main() { i j j j


nt x = 5; in j j j j


t y = 7; if ( j j j j j


x > y) j j


printf("x is greater than y\n"); r j j j j j


eturn 0; j

}
Answer: The if statement does not have braces around the body, so only the pr
j j j j j j j j j j j j j j


intf statement will be executed if x is greater than y. This can lead to unexpect
j j j j j j j j j j j j j j j


ed behavior and should be avoided. The code should be modified as follows:
j j j j j j j j j j j j




int main() { in j j j


t x = 5; int y j j j j j j


= 7; if (x > y j j j j j


){ j


printf("x is greater than y\n"); j j j j


}
return 0; j


}
7. What is wrong with the following code? basi
j j j j j j j j


c

int main() { in j j j


t x = 5; int y j j j j j j


= 7; if (x > y j j j j j


){ j


Oops, it looks like there was a mistake in my previous response. Here are the re
j j j j j j j j j j j j j j j


maining answers: j




7. What is wrong with the following code?
j j j j j j j




int main() { i j j j


nt x = 5; int y
j j j j j


= 7;
j j


if (x > y) {
j j j j


printf("x is greater than y\n"); j j j j

,} else if (x < y) {
j j j j j j


printf("x is less than y\n"); j j j j


}
return 0; j


}


Answer: There is nothing wrong with this code. j j j j j j j




8. What is wrong with the following code?
j j j j j j j




int main() { i j j j


nt x = 5; int y
j j j j j


= 7;
j j


if (x > y) {
j j j j


printf("x is greater than y\n"); j j j j


} else if (x == y) { printf("x i
j j j j j j j j


s equal to y\n");
j j j


} else {
j j


printf("x is less than y\n"); j j j j


}
return 0; j


}


Answer: There is nothing wrong with this code. j j j j j j j




9. What is wrong with the following code?
j j j j j j j




int main() { i j j j


nt x = 5; int y
j j j j j j


= 7; switch (
j j j


x){ case 1: j j j


printf("x is 1\n"); break; j j j


case 2: j


printf("x is 2\n"); break; j j j


default:
printf("x is not 1 or 2\n"); j j j j j


}
returnAnswer: There is nothing wrong with this code. j j j j j j j

, 10.What is wrong with the following code? j j j j j j




int main() { i
j j j


nt i = 0;
j j j


while (i < 10) { printf("
j j j j j


%d\n",i); j


}
return 0; j


}
Answer: The while loop is an infinite loop because the value of i is never incre
j j j j j j j j j j j j j j j


mented. It should be modified as follows:
j j j j j j




int main() { i
j j j


nt i = 0;
j j j


while (i < 10) { printf
j j j j j


("%d\n",i); i++; j j


}
return 0; j


}

11.What is wrong with the following code? j j j j j j




int main() { i
j j j


nt i;j


for (i = 0; i < 10; i++) { pri
j j j j j j j j j


ntf("%d\n", i); i++; j j


}
return 0; j


}

Answer: The variable i is incremented twice in each iteration of the for loop, w
j j j j j j j j j j j j j j


hich means that only half of the expected numbers will be printed. The i++ state
j j j j j j j j j j j j j j


ment inside the for loop should be removed:
j j j j j j j




int main() { i
j j j


nt i;j


for (i = 0; i < 10; i++) { printf("%
j j j j j j j j j


d\n", i); j


}
return 0; j


}
12.What is wrong with the following code? j j j j j j

Connected book
 image
Publisher: 2017 ISBN: 9781337102087 Edition: Unknown

Document information

Uploaded on
April 17, 2025
Number of pages
58
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$21.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
TESTBANKSELLER
4.8
(842)
Sold
15
Followers
2
Items
498
Last sold
5 months ago


Reviews from verified buyers




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions