Question

In: Computer Science

JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...

JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!!

QUESTION 1

  1. Consider the following program segment:

    int i = 2;
    int n = 18;

    while (i < n)
    {
        if ((i % 2) == 0)
    i++;
    else
    n--;
    }
    System.out.println( i );

    What is the value of n when the above program segment is done executing?

    A.

    n is: 18.

    B.

    n is: 3.

    C.

    Infinite Loop. i will not change.

    D.

    No Output.

QUESTION 2

  1. Consider the following program segment:

    int i = 2;
    int n = 18;

    while (i < n)
    {
        if ((i % 2) == 0)
    i++;
    else
    n--;
    }
    System.out.println( i );

    What is the output of the above program segment?

    A.

    There is no output.

    B.

    The output is 2.

    C.

    The output is 18.

    D.

    The output is 3.

QUESTION 3

  1. Consider the following program segment:

    int i = 1;
    int n = 10;

    while (i < n)
    {
        if ((i % 2) != 0)
    i++;
    }
    System.out.println ( i );

    And what are the values of i and n after execution?

    A.

    i will be 11. n will be 10.

    B.

    i will be 11. n will be 1.

    C.

    Infinite Loop: i is stuck at 2 and n stays at 10.

    D.

    No Output

QUESTION 4

  1. Consider the following section of code:

    int i = 1;
    int j = 3;
    while (i < 15 && j < 20)
    {
    i++;
    j += 2;
    }
    System.out.println (i + j);

    And what are the values of i and j after execution?

    A.

    i = 10, j = 21

    B.

    i = 11, j = 22

    C.

    i = 15, j = 20

    D.

    i = 16, j = 21

QUESTION 5

  1. Consider the following section of code:

    int i = 1;
    int j = 3;
    while (i < 15 && j < 20)
    {
    i++;
    j += 2;
    }
    System.out.println (i + j);

    What is the output of the above program segment?

    A.

    The output will be 31.

    B.

    The output will be 33.

    C.

    The output will be 34.

    D.

    The output will be 35.

QUESTION 6

  1. Consider the following section of code:

    int x = 10;
    do
    {
       System.out.println( x );
    x -= 3;
    }
    while (x > 0);

    And what is the value of x after execution?

    A.

    The value of x after loop is done = -1.

    B.

    The value of x after loop is done = -2.

    C.

    The value of x after loop is done = 1.

    D.

    The value of x after loop is done = 2.

QUESTION 7

  1. Consider the following section of code:

    int x = 10;
    do
    {
       System.out.println( x );
    x -= 3;
    }
    while (x > 0);

    What is the output of the following program segment?

    A.

    -2

    B.

    10 9 8 7 6 5 4 3 2 1

    C.

    1 4 7 10

    D.

    10

    7

    4

    1

QUESTION 8

  1. Consider the following section of code:

    int i = 1;
    int n = 0;
    while (i <= n)
    {
    System.out.print( i );
    i++;
    }

    And what are the values of i and n after execution?

    A.

    i = 1 and n = 0.

    B.

    i = 2 and n = 0.

    C.

    i = 0 and n = 0.

    D.

    i = -1 and n = 0.

QUESTION 9

  1. Consider the following section of code:

    int i = 1;
    int n = 0;
    while (i <= n)
    {
    System.out.print( i );
    i++;
    }

    What is the output of the following program segment?

    A.

    1, 2, 3, ...

    B.

    1

    C.

    0

    D.

    No output.

Solutions

Expert Solution

Question 1:

For the first iteration, 2<18, and 2%2 == 0 returns true, so 'i' is incremented. After that for every iteration,  i % 2 won't be equal to zero and hence 'n' wud keep on decreasing. When n=3, loop will end. So, after executing, both i and n are equal to 3.

So, (B) n is: 3 is the correct answer.

Question 2:

The code is same as question 1, so the explanation remains the same. 'i' won't increase in value after the first iteration, so it will be equal to 3 at the end.

So, (D) The output is 3 is the correct answer.

Question 3:

During the first iteration, i<n returns true so the loop starts. (1%2 != 0) returns true, so the value of i is incremented to 2. For all the next iterations, the i<n remains true, whereas ( i%2 != 0 ) returns false because i=2. Due to this, the value of i never changes and the loop goes on infintely. The value of 'n' never changes.

So, (C) Infinite Loop: i is stuck at 2 and n stays at 10 is the correct answer.

Question 4:

At start, the condition i<15 and j<20 satisfies so the loop starts. For every iteration, we increment i by 1 and j by 2.

Value after iteration1: i=2 j=5
Value after iteration2: i=3 j=7
Value after iteration3: i=4 j=9
Value after iteration4: i=5 j=11
Value after iteration5: i=6 j=13
Value after iteration6: i=7 j=15
Value after iteration7: i=8 j=17
Value after iteration8: i=9 j=19
Value after iteration9: i=10 j=21

After the 9th iteration, the condition of loop (i<15 && j<20) will return false and the loop will end. Hence, the final values are 10 and 21.

So, (A) i = 10, j = 21 is the correct answer.

Question 5:

This has the same code as in Q4. At the end i is 10 and j is 21. Their sum will be 10+21 = 31.

So, (A) The output will be 31 is the correct answer.

Question 6:

Value of x during iteration 1: 10
Value of x during iteration 2: 7
Value of x during iteration 3: 4
Value of x during iteration 4: 1

After this, this value is decremented by 3 and it becomes -2. As -2 is not greater than 0 so the while condition gets false and loop stops.

So, (B) The value of x after loop is done = -2 is the correct answer.

Question 7:

The program code is same as Q6. For every iteration, the value of x is printed, which are 10, 7, 4, and 1.

So, (D) 10
7
4
1
is the correct answer.

Question 8:

The entry condition of the loop is i<=n. This never gets true because i is 1 and n is 0. The loop never gets executed and the value of both the variables do not change.

So, (A) i = 1 and n = 0 is the correct answer.

Question 9:

The problem code is same as Q8. As discussed there, the while loop never gets executed so no print statement gets executed. The program gives no output.

So, (D) No output is the correct answer.


Related Solutions

I know this is more than one question. I'm hoping you can address these all at...
I know this is more than one question. I'm hoping you can address these all at once? How are the ways in which organizations choose to measure and evaluate the performance of their segments tied to how managers of those segments get evaluated? What are the ways those approaches can fairly evaluate managers? How can those approaches sometimes unfairly evaluate managers? Can people "game" this system? If so, how? What can be done to ensure both accurate segment performance evaluation...
JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared...
JAVA JAVA JAVA JAVA, How to determine if there is more than one number that appeared the X amount of times. (For example there are 3 numbers which occured 50 times so I want the output to show which 3 numbers occured 50 times) This is the code import java.util.Random; public class MostLeastOccurring { public static void main(String[] args) { Random random = new Random(); int x = 1000; int[] array = new int[x]; for (int i = 0; i...
JAVA question A string may use more than one type of delimiter to bracket information into...
JAVA question A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters. A string is considered properly delimited if each right delimiter ] } or )( is matched with a preceding left delimiter { [ ( of the same type in such a way that either the resulting blocks of...
if this is considered more than one question then please explain part 'I' (Find the exact...
if this is considered more than one question then please explain part 'I' (Find the exact value of f100, f500, and f1000, where fn is the nth Fibonacci number. What are times taken to find out the exact values?). I am having trouble writing the code for both recursive and iterative functions without having the program crash. I assume its because the numbers are so high. Goal: The main goal of the project is to let students use their prior...
This is the first question and I know how to solve this one, but I am...
This is the first question and I know how to solve this one, but I am confused by the second one (The admissions office of a small, selective liberal-arts college will only offer admission to applicants who have a certain mix of accomplishments, including a combined SAT score of 1,300 or more. Based on past records, the head of admissions feels that the probability is 0.58 that an admitted applicant will come to the college. If 500 applicants are admitted,...
I've seen this question answered for C but I don't know how to translate to Java....
I've seen this question answered for C but I don't know how to translate to Java. Write a program that requests the hours worked in a week and the basic pay rate, and then prints the gross pay (before tax), the taxes, and the net pay (after tax). Assume the following: Basic pay rate: from user's choice Overtime (in excess of 40 hours) = time and a half (i.e., if the hours worked is 45, the hours for pay will...
There can be more than 1 right answer. Is it B and C both? I know...
There can be more than 1 right answer. Is it B and C both? I know it happens by diffusion and it says passive transport. But proteins are required too? or does it just work with diffusion. 17. Reabsorption of H2O in the medullary collecting ducts is by A. active transport B. simple diffusion C. facilitated diffusion D. endocytosis
Disease: Leukemia Is there more than one type of disease? If so, what are they? What...
Disease: Leukemia Is there more than one type of disease? If so, what are they? What are the signs and symptoms of the disease? What are the possible treatments for the disease? What would you recommend to a patient who has been diagnosed with the disease? Can anything be done to prevent the disease?
I need more than just one question answered, please... 1. Which component of speech acts is...
I need more than just one question answered, please... 1. Which component of speech acts is the most difficult to determine? a. the linguistic form b. the context of the message c. the effect on the listener d. the intent of the message 2. If you tell a friend about a movie you watched the previous night, you would be engaging in a a. speech act. b. social register. c. narrative. d. conversation. 3. According to Bates, if a child...
Are we taking more vitamin supplements than our parents? If so or not I would like...
Are we taking more vitamin supplements than our parents? If so or not I would like you to please analyze how lifestyle changes between our parents and ours that have influenced this behavior of taking more or less vitamin supplements.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT