Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 9 years ago

A couple of Java questions?

Q19. Which of the following conditions does not test whether x is between 1 and 10 (inclusive)?

A) 1 <= x && x <= 10

B) !(x < 1 || 10 < x)

C) !(x <= 1 || x >= 10)

D) 10 >= x && x >= 1

Q20. Which of the following statements is correct?

A) It is a good idea to write the first test cases before the program is written completely.

B) It is not a good idea to write the first test cases before the program is written completely.

C) It is not a good idea to write the first test cases after the program is written completely.

D) It is a good idea to write the first test cases after the program is written completely.

Q23. The statement y = x >= ? x : -x; is similar to ____.

A) if,(x >= 0) y = x;

B) if,(x >= 0) y = x; else y = -x;

C) if,(x >= 0) y = -x;

D) if,(x >= 0) y = -x; else y = x;

Q35. Which of the following code fragments is considered bad style?

a)

public int getYears()

{

return years;

}

b)

for(int i = 1; i <= years; i++)

{

if(balance >= targetBalance)

i = years + 1;

else

{

double interest = balance * rate / 100;

balance = balance + interest;

}

}

c)

public double getBalance()

{

return balance;

}

d)

for (int i = 1; i <= n; i++)

{

double interest = balance * rate / 100;

balance = balance + interest;

}

2 Answers

Relevance
  • Anonymous
    9 years ago
    Favorite Answer

    Q19: C

    Reason:

    A) 1 <= x && x <= 10

    is the same as 1 <= x <= 10 showing x is between 1 and 10 inclusive

    B) !(x < 1 || 10 < x)

    The || means this will return true if x is either less than 1 OR x is greater than 10. The ! in front reverses the answer so it will return the same result as A.

    C) !(x <= 1 || x >= 10)

    This one is the correct answer because although it tests if x is between 1 and 10 it is exclusive

    D) 10 >= x && x >= 1

    This is just like A, just reverse the position of the conditions

    Q20: A and/or C

    Reason: Aren't A & C the same and B & D the same answers?

    You want to write the test case before completing coding to ensure you understand the problem being solved.

    Q23: B

    Reason:

    y = x >= 0 ? x : -x; This assigned to y the result of a shorthand if statement.

    y = {condition to test, (x >= 0)} ? {value if true, (x)} : {value if false, (-x)};

    This example is a way of assigning the absolute value of x to y.

    Q35: B

    Reason: This code is not immediately understandable, and also not efficient.

    Source(s): I'm an SCJP
    • Login to reply the answers
  • 9 years ago

    // HOW are you going to learn java if you don't do the homework?

    Q19 has a typo error somewhere. None of the answers satisfies, but I can tell you that || is not one of the answers

    Q20 is A obviously

    Q23 is B

    Q35 is B, why go through all that extra logic to cancel the loop?

    if( balance <= targetBalance) {

    double interest = balance * rate / 100;

    balance += interest;

    }

    }

    • Login to reply the answers
Still have questions? Get your answers by asking now.