• Post Reply Bookmark Topic Watch Topic
  • New Topic

help with assignment/quiz  RSS feed

 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I even put it into jGrasp but still sometimes get questions wrong.
please help

What is the value of j when the above code is done executing?   Assume there are no syntax errors and the code will compile.

int j;
int total = 50;
for (j = 1; j < 6; j++)
     total = total + j;

A.
6

B.
0

C.
1

D.
55

2 points   Save Answer
QUESTION 2

What is the value of total when the loop is done?  Assume there are no syntax errors and the code will compile.

int j;
int total = 50;
for (j = 1; j < 6; j++)
     total = total + j;

A.
55

B.
65

C.
50

D.
0

2 points   Save Answer
QUESTION 3

What will be printed when the user enters 10 for x and 0 for y?  Assume there are no syntax errors and the code will compile.

int x;
int y;

System.out.println("Please enter a value for x: ");
x = scan.nextInt();
System.out.println("Please enter a value for y: ");
y = scan.nextInt();

while (x > y)
{
    x -= 3;
    System.out.print (x + " ");
}
System.out.println();

A.
0 3 6 9

B.
10 7 4 1

C.
7 4 1

D.
7 4 1 -2

2 points   Save Answer
QUESTION 4

What is the value of x when the code is done executing when the user enters 10 for x and 0 for y?  Assume there are no syntax errors and the code will compile.

int x;
int y;

System.out.println("Please enter a value for x: ");
x = scan.nextInt();
System.out.println("Please enter a value for y: ");
y = scan.nextInt();

while (x > y)
{
    x -= 3;
    System.out.print (x + " ");
}
System.out.println();

A.
-1

B.
-2

C.
10

D.
0

2 points   Save Answer
QUESTION 5

What is the value of y when the code is done executing when the user enters 10 for x and 0 for y?  ?  Assume there are no syntax errors and the code will compile.

int x;
int y;

System.out.println ("Enter a value for x:  ");
x = scan.nextInt();

System.out.println ("Enter a value for y:  ");
y = scan.nextInt();

while (x > y)
{
    x -= 3;
    System.out.print (x + " ");
}
System.out.println();

A.
0

B.
10

C.
-1

D.
-2

2 points   Save Answer
QUESTION 6

What are the values of count after the following code is executed?  Assume there are no syntax errors and the code will compile.

int count = 5;
int num = 3;

do
{
    if (count % 2 != 0)
        num -= 4;
    else
        num += 2;
    count++;
}
while (count < 12);


A.
0

B.
5

C.
13

D.
12

2 points   Save Answer
QUESTION 7

What is the value of num when the loop is done?  Assume there are no syntax errors and the code will compile.

int count = 5;
int num = 3;

do
{
    if (count % 2 != 0)
        num -= 4;
    else
        num += 2;
    count++;
}
while (count < 12);

A.
3

B.
12

C.
-7

D.
0

 
Jesse Matty
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for questions 1234567 the answer is none of the above
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of

Did you try to compile and execute the code and print out the variable's value to see what the code did?

Please wrap all  posted code segments in code tags to preserve formatting and make the code easier to read.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get errors with the code that you need help with, post the code in code tags and the full text of the error messages.
 
Tim Cooke
Marshal
Posts: 4038
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the purposes of avoiding copyright infringement please provide attribution for the questions and code you've presented. i.e: tell us where you got these questions from?
 
Daniel Ortega
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really new at this (rookie) but I *think* I get what is going on here.



The first time the loop executes, j=1. Then after the loop end, j is incremented by 1, so the next time the loop executes, j=2, then 3, then 4, and finally 5. So that after the 5th time the loop executes, 'total' = 65. basically like this:

total=50+1+2+3+4+5; //65

At least that's how I believe this works.

Thanks.
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:For the purposes of avoiding copyright infringement please provide attribution for the questions and code you've presented. i.e: tell us where you got these questions from?


blackboard learn csc 111
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Radder wrote:If you get errors with the code that you need help with, post the code in code tags and the full text of the error messages.


there is no error.  it doesnt print anything out
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ortega wrote:I'm really new at this (rookie) but I *think* I get what is going on here.



The first time the loop executes, j=1. Then after the loop end, j is incremented by 1, so the next time the loop executes, j=2, then 3, then 4, and finally 5. So that after the 5th time the loop executes, 'total' = 65. basically like this:

total=50+1+2+3+4+5; //65

At least that's how I believe this works.

Thanks.


thats not even one of the choices
 
Jesse Matty
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ed Palazo wrote:
Daniel Ortega wrote:I'm really new at this (rookie) but I *think* I get what is going on here.



The first time the loop executes, j=1. Then after the loop end, j is incremented by 1, so the next time the loop executes, j=2, then 3, then 4, and finally 5. So that after the 5th time the loop executes, 'total' = 65. basically like this:

total=50+1+2+3+4+5; //65

At least that's how I believe this works.

Thanks.




it ask for j not total and j would be 5  which again not a choice

thats not even one of the choices
 
Daniel Ortega
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jesse, you are correct. 'j' does = 5, my bad. So 'total' = 65 and 'j' = 5, but that is not one of the choices listed in original post.

I was just trying to make sure I understood how this for loop works, and how 'j' and 'total' came out to be 5 and 65.

Thanks.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesnt print anything out

Then you need to add the necessary print statement to show the values in question.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ortega wrote:. . . 'j' = 5, but that is not one of the choices listed in original post. . . .
5 is not listed because 5 is wrong.

Please you who are new to this forum, don't simply give out answers. Note what it says at the beginning of this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
It doesn't help to give complete solutions too early; the person answering the question will remember better what they worked out for themselves.
 
Ed Palazo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nvm i solved problems.  got a 100.  I am a dumbass actually, I wasnt using System.out.print to print out wha I wanted to print lol.  simple mistake
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!