Results 1 to 7 of 7
Thread: If Statements
- 02-02-2012, 04:59 AM #1
If Statements
I am working on the practice multiple choice problems for the AP Computer Science exam.
I came across this question:
4. At a certain high school students receive letter grades based on the following
scale.
Numeric Score Letter Grade
93 or above A
From 84 to 92 inclusive B
From 75 to 83 inclusive C
Below 75 F
Which of the following code segments will assign the correct string to grade
for a given integer score ?
I. if (score .5 93)
grade 5 "A";
if (score .5 84 && score ,5 92)
grade 5 "B";
if (score .5 75 && score ,5 83)
grade 5 "C";
if (score , 75)
grade 5 "F";
II. if (score .5 93)
grade 5 "A";
if (84 ,5 score ,5 92)
grade 5 "B";
if (75 ,5 score ,5 83)
grade 5 "C";
if (score , 75)
grade 5 "F";
III. if (score .5 93)
grade 5 "A";
else if (score .5 84)
grade 5 "B";
else if (score .5 75)
grade 5 "C";
else
grade 5 "F";
(a) II only
(b) III only
(c) I and II only
(d) I and III only
(e) I, II, and III
Letter D is the correct answer. My question is why is II wrong? Wouldn't that code execute fine or is there a problem with the if Statements?
- 02-02-2012, 05:25 AM #2
Re: If Statements
What language is that code? it certainly isn't valid Java code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-02-2012, 05:26 AM #3
Re: If Statements
well it's java. but this isn't the entire code. its just the question. its like a part of a bigger program we don't get to see i guess. We are just supposed to say whether I, II, III or all of them would complete the task. Does that clear things up?
- 02-02-2012, 06:11 AM #4
Re: If Statements
Why do they call it rush hour when nothing moves? - Robin Williams
- 02-02-2012, 06:13 AM #5
Re: If Statements
At a guess, I would say you lazily copied that from a source which used images for mathematical operators, and the images didn't get pasted here. And you didn't bother to replace them with their textual equivalents either.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-02-2012, 06:17 AM #6
Computer Science Student
- Join Date
- Feb 2012
- Location
- Savannah, GA
- Posts
- 4
- Rep Power
- 0
Re: If Statements
I think the problem is with the logic in the if statement parameters. Someone correct me if I'm wrong, but you can't chain logic the way it's chained in II. You have to define the left separate from the right. That is, you can't use "5 < x < 10" but have to state "5 < x" and "x < 10."
--SM
- 02-02-2012, 08:06 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
if else if statements
By Allspark in forum New To JavaReplies: 9Last Post: 09-28-2010, 06:50 PM -
Help with if else statements
By np2392 in forum New To JavaReplies: 2Last Post: 09-24-2010, 01:25 AM -
if else statements
By sweetpea123 in forum New To JavaReplies: 4Last Post: 04-12-2010, 07:02 PM -
age: using if statements
By yasmin k in forum New To JavaReplies: 2Last Post: 10-04-2009, 09:50 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks