IU Bloomington Evaluation Services and Testing

Indiana University
Bloomington Evaluation Services and Testing

QuizSite  Students
Quizzes · Tests · Evaluations · Surveys
Activity: Practice_Eight
Student: dgerman (Adrian German)
Instructor: dgerman
Course: J201 Section: 4285
Question 1.
Consider the following code fragment.
if (x > 0 && x < 10)                 // line 1
   y = x;                            // line 2
else                                 // line 3
   if (x <= 0 || x >= 10)            // line 4
      y = 2 * x;                     // line 5
   else                              // line 6
      y = 3 * x;                     // line 7
How can you simplify it without changing its semantics (meaning, purpose)?

Remove lines 4, 5, and 6.
Remove lines 4, 6, and 7.
Remove lines 3, 4, and 5.
Remove line 3.
Move line 7 above line 1, and remove lines 3, 4, 5, and 6.

If more than one of the suggestions above result in a correct simplification of the code please choose the answer that results in the biggest simplification (the one that simplifies the most).


Question 2.
Assume that x is an integer variable. Simplify the following boolean expression:
(x < 5) && (x < 25)

x < 5
5 < x < 25
!(x > 5)
x < 25
none of the above


Question 3.
Assume that x is an integer variable. Simplify the following boolean expression:
(x < 5) || (x < 25)

x < 5
5 < x < 25
!(x > 5)
x < 25
none of the above


Question 4.
Assume that x is an integer variable. Simplify the following boolean expression:
(x > 3) || (x < 5)

x == 4
true
x > 3
x < 5
none of the above

Hints: Try to answer the following questions first:


Question 5.
Assume that x is an integer variable. Simplify the following boolean expression:
(x > 3) && (x < 5)

x == 4
true
x > 3
x < 5
none of the above


Question 6.
Consider the following two program fragments. Assume that x is an int variable.
// fragment 1    |         // fragment 2
                 |
if (x == 5)      |         if (x == 5)
   x = x + 1;    |            x = x + 1;
else             |         if (x != 5)
   x = 8;        |            x = 8;
Which of these statements is true?

The two fragments are logically equivalent.
If x is 6 initially, then
Fragment 2 contains a syntax error.
x always has the value 8 after executing fragment 2.
x has either the value 5 or the value 8 after executing fragment 1.


Question 7.
Assume that x and y are integer variables. Consider the following nested if statement.
if (x > 3) {
   if (x <= 5) y = 1;
   else if (x != 6) y = 2;
        else y = 3;
} else y = 4;
If y has the value 2 after executing the above program fragment, then what do you know about x?

(x == 4) || (x == 6)
x >= 7
(x != 6) && (x < 5)
(x <= 3)
none of the above


Question 8.
true is an example of

a boolean constant.
a String constant.
both a boolean constant and a String constant.
an undeclared variable.
a type.

Hints: Try to answer the following questions first:


Question 9.
"5" is an example of

an int constant.
a String constant.
a char constant.
both an int constant and a char constant.
an int constant, a String constant, and a char constant.

Hints: Try to answer the following questions first:


Question 10.
Assume that x is an integer variable. Simplify the following boolean expression.
!(((x > 0) || (x == 0)) && (x < 10))
Warning: Don't overlook the logical negation (bang!) at the front.

true
(x != 0) || (x >= 10)
false
(x < 0) || (x >= 10)
(x < 0) && (x >= 10)


Question 11.
Assume that x is an integer variable. Simplify the following boolean expression.
!(((x > 0) || (x == 0)) || (x < 10))
Warning: Don't overlook the logical negation (bang!) at the front.

true
(x != 0) || (x >= 10)
false
(x < 0) || (x >= 10)
(x < 0) && (x >= 10)


Question 12.
The legal grades are 'a', 'b', 'c', 'd', and 'f'.

Given the following declarations:

boolean legal; char grade;
Which of the following code fragments will assign legal

Check all that apply.


A:
if (grade >= 'a' && grade <= 'f')
   if (grade != 'e') legal = true;
   else legal = false;
else legal = false; 

B:
legal = false;
if (grade >= 'a' && grade <= 'd') legal = true;
else if (grade == 'f') legal = true;

C:
legal = (grade >= 'a' && grade <= 'd') || grade == 'f'; 

D:
legal = (grade >= 'a' && grade <= 'f') || grade != 'e'; 

E:
if (grade.equals('a')) legal = true;
else if (grade.equals('b')) legal = true;
else if (grade.equals('c')) legal = true;
else if (grade.equals('d')) legal = true;
else if (grade.equals('f')) legal = true;
else legal = false; 


Question 13.
Consider the following program fragment.
int x = 6, y = 7;
x += y; ++y; System.out.println(x + " " + y); 
What is printed when the fragment is executed?

13 8
7 8
6 7
13 7
7 7

Hint: Remember that:


Question 14.
Given the following declaration:
String picard = "Engage!";
What does picard.charAt(3) evaluate to?

"a"
'a'
1
"age!"
97

Hints: Try to answer these questions first:


Question 15.
Which of the following expressions does not evaluate to 3?

'M' - 'J'
(4 * 5 + 2) / 7
"012".length()
8 - 3 - 2
'3'

Hint: Try to answer these questions first:


Question 16.
After the following statement is executed,
int x = (int)(Math.random() * (12 - 5) + 6)
which of these numbers could not possibly be contained in x?

6
12
5
9
8


Question 17.
Which of the following are infinite loops? Check all that apply.

int count = 0;
while (count > 0) {
   System.out.println(count);
   count = count + 1;
}

for (int i = 0; i < 10; --i) {
   System.out.println(i);
}

for (int i = 1; i != 10; i = i + 2) {
   System.out.println(i);
}

int x = 0;
if (x >= 0) {
   System.out.println(x);
   ++x;
} 

int x = 0;
while (x == 0) {
   x = x + 1;
   System.out.println(x);
   x = 0;
}


Question 18.
What is printed by the following program?
public class Quiz {
  public static void main(String[] args) {
    System.out.println(enough(125));
  }
  public static String enough(int credits) {
    String ans = "false";
    if (credits >= 150)
       ans = "true";
    return ans;
  }
}

"true"
false true
true
false
impossible to determine from the information given


Question 19.
Consider the following code fragment when embedded in a complete program:
if (x > 3) {
  if (x <= 5)
    y = 1;
  else if (x != 6)
    y = 2;
} else
    y = 3;
Assume that x has a value of 6 at the beginning of the fragment.

What value does the variable y hold after the fragment gets executed?


1
2
3
6
impossible to determine from the information given


Question 20.
Captain Picard is trying to write a program to calculate the number of vowels in a string. He is having trouble figuring out what condition to use in his if statement.
public class Quiz {
  public static void main(String[] args) {
    String sentence = "Where no one has gone before...";
    int len = sentence.length();
    int pos = 0, cnt = 0;
    while (pos < len) {
      if (________________________________________) {
         ++cnt;
      }
      ++pos;
    }
    System.out.println("The sentence contains " + cnt + " vowels.");
  }
  public static boolean isVowel(char c) {
    boolean ans = false;
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
       ans = true;
    return ans;
  }
}
The future of the universe hangs in the balance. Picard calls you to the bridge for help. Which of the following conditions do you advise him to use?

isVowel(sentence.charAt(pos))
isVowel(sentence.substring(pos, pos + 1))
sentence.substring(pos, pos + 1).equals("aeiou")
sentence.isVowel(pos)
sentence.isVowel(sentence.charAt(pos))


Question 21.
A method that
returns the value of an internal (instance) variable
can be classified as which of the following?

An accessor method
A mutator method
An effector method


Question 22.
A method that
exists to modify the value of an internal (instance) variable
can be classified as which of the following?

An accessor method
A mutator method
An effector method


Question 23.
A method that
exists to perform an action
can be classified as which of the following?

An accessor method
A mutator method
An effector method


Question 24.
Consider the following 'nonsense' class.
class A
{ public A() { n = 0; }
  public A(int a) { n = a; }
  public void f() { n++; }
  public void g() { f(); n = 2 * n; f(); }
  public int h() { return n; }
  public void k() { System.out.println(n); }
  private int n;
}
Which of the following statements is not true about the class.


Class A does not have a default no-arg constructor.
Class A defines 5 instance members.
Objects of type A have at least one mutator.
Objects of type A have at least one accessor.
Objects of class A have no effectors.


For the following 5 (five) questions consider the following method definitions:
public static double f(double x) {
  return g(x) + h (h (x));
}
public static double g(double x) { return 4 * h (x) ; }
public static double h(double x) { return x * x + k (x) - 1; }
public static double k(double x) { return 2 * (x + 1); }
Now here are the questions:
Question 25.
What does f(2) evaluate to?

1.0
185.0
92.0
400.0
none of the above

Question 26.
What does g(h(2)) evaluate to?


1.0
185.0
92.0
400.0
none of the above

Question 27.
What does k(g(2) + h(2)) evaluate to?


1.0
185.0
92.0
400.0
none of the above

Question 28.
What does f(0) + f(1) + f(2) evaluate to?


1.0
185.0
92.0
400.0
none of the above

Question 29.
What does f(-1) + g(-1) + h(-1) + k(-1) evaluate to?


1.0
185.0
92.0
400.0
none of the above


Consider the following method:
int fun(int m) {
  int n = 0;
  if (m == 0) {
    n = 1;
  } else {
    n = m + fun(m - 2);
  }
  return n;
} 
Please answer the following two questions:
Question 30.
What does fun(4) evaluate to?

4
5
7
it never ends
none of the above

Question 31.
What does fun(5) evaluate to?


4
5
7
it never ends
none of the above


Question 32.
The header for a function named doSomething looks like this:

public static String doSomething(String s, char c)
Which of the following statements does not contain a valid call to the function doSomething?

doSomething("a", 'a');
int len = doSomething("a", 'a').length();
doSomething(doSomething("a", 'a'), 'a');
String s = doSomething('a', "a");
String s = 'a' + doSomething("a", 'a');


Click to submit your answers.   Help


This activity is available between these dates and times: 07.08.10:00-08.11.19:15
It can be submitted a total of 30 times.
If it is a scored activity, the highest non-zero score will be counted.


Screen: TAKE_QUIZ
Last updated: Thu Mar 8 13:40:07 2001
URL: https://www.indiana.edu/~best/cgi-bin/quizsite/students.pl
Comments: best@indiana.edu
Copyright 2000, BEST & The Trustees of Indiana University