CPT-236-Java-Test 2

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/76

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

77 Terms

1
New cards

What is the value of balance after the following code is executed?

int balance = 10;

while (balance >= 1) {

   if (balance < 9)

      break;

   balance = balance - 9;

}

1

2
New cards

Is the following loop correct?

for ( ; ; );

Yes

3
New cards

How many times is the println statement executed?

for (int i = 0; i < 10; i++)

   for (int j = 0; j < i; j++)

     System.out.println(i * j)

45

4
New cards

What will be displayed when the following code is executed?

int number = 6;

while (number > 0) {

   number -= 3;

   System.out.print(number + " ");

}

3 0 -3

5
New cards

What is the output of the following code?

int x = 0;

while (x < 4) {

   x = x + 1;

}

System.out.println("x is " + x);

x is 4

6
New cards

How many times will the following code print "Welcome to Java"?

int count = 0;

while (count < 10) {

   System.out.println("Welcome to Java");

   count++;

}

10

7
New cards

The following loop displays _______________.

for (int i = 1; i <= 10; i++) {

   System.out.print(i + " ");

   i++;

}

1 3 5 7 9

8
New cards

How many times will the following code print "Welcome to Java"?

int count = 0;

do {

   System.out.println("Welcome to Java");

   count++;

} while (count < 10);

10

9
New cards

How many times is the println statement executed?

for (int i = 0; i < 10; i++)

   for (int j = 0; j < 10; j++)

     System.out.println(i * j);

100

10
New cards

Which of the following loops prints "Welcome to Java" 10 times?

A:

for (int count = 1; count <= 10; count++) {

   System.out.println("Welcome to Java");

}

B:

for (int count = 0; count < 10; count++) {

   System.out.println("Welcome to Java");

}

C:

for (int count = 1; count < 10; count++) {

   System.out.println("Welcome to Java");

}

D:

for (int count = 0; count <= 10; count++) {

   System.out.println("Welcome to Java");

}

BD

11
New cards

To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?

add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.

12
New cards

How many times will the following code print "Welcome to Java"?

int count = 0;

do {

   System.out.println("Welcome to Java");

} while (++count < 10);

10

13
New cards

What is the output after the following loop terminates?

int number = 25;

int i;

boolean isPrime = true;

for (i = 2; i < number; i++) {

   if (number % i == 0) {

      isPrime = false;

     break;

   }

}

System.out.println("i is " + i + " isPrime is " + isPrime);

i is 5 isPrime is false

14
New cards

Do the following two statements in (I) and (II) result in the same value in sum?

(I):

for (int i = 0; i < 10; ++i) {

   sum += i;

}

(II):

for (int i = 0; i < 10; i++) {

   sum += i;

}

Yes

15
New cards

What is the number of iterations in the following loop?

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

   // iteration

}

n - 1

16
New cards

How many times will the following code print "Welcome to Java"?

int count = 0;

while (count++ < 10) {

   System.out.println("Welcome to Java");

}

10

17
New cards

What is the output for y?

int y = 0;

for (int i = 0; i < 10; ++i) {

   y += i;

}

System.out.println(y);

45

18
New cards

What is the value in count after the following loop is executed?

int count = 0;

do {

   System.out.println("Welcome to Java");

} while (count++ < 9);

System.out.println(count);

10

19
New cards

What is y after the following for loop statement is executed?

int y = 0;

for (int i = 0; i < 10; ++i) {

   y += 1;

}

10

20
New cards

__________ is a simple but incomplete version of a method.

A stub

21
New cards

__________ is to implement one method in the structure chart at a time from the top to the bottom.

Top-down approach

22
New cards

You should fill in the blank in the following code with ______________.

public class Test {

   public static void main(String[] args) {

     System.out.print("The grade is ");

      printGrade(78.5);

     System.out.print("The grade is ");

      printGrade(59.5);

   }

   public static __________ printGrade(double score) {

     if (score >= 90.0) {

     System.out.println('A');

     }

     else if (score >= 80.0) {

       System.out.println('B');

     }

     else if (score >= 70.0) {

       System.out.println('C');

     }

     else if (score >= 60.0) {

       System.out.println('D');

     }

     else {

       System.out.println('F');

     }

   }

}

void

23
New cards

Analyze the following code:

public class Test {

   public static void main(String[] args) {

     System.out.println(xMethod(5, 500L));

   }

   public static int xMethod(int n, long l) {

     System.out.println("int, long");

     return n;

   }

   public static long xMethod(long n, long l) {

     System.out.println("long, long");

     return n;

   }

}

The program displays int, long followed by 5.

24
New cards

You should fill in the blank in the following code with ______________.

public class Test {

   public static void main(String[] args) {

     System.out.print("The grade is " + getGrade(78.5));

     System.out.print("\nThe grade is " + getGrade(59.5));

   }

   public static _________ getGrade(double score) {

     if (score >= 90.0)

        return 'A';

     else if (score >= 80.0)

        return 'B';

     else if (score >= 70.0)

        return 'C';

     else if (score >= 60.0)

        return 'D';

     else

        return 'F';

   }

}

char

25
New cards

(int)(Math.random() * (65535 + 1)) returns a random number __________.

between 0 and 65535

26
New cards

Arguments to methods always appear within __________.

parentheses

27
New cards

What is k after the following block executes?

{

   int k = 2;

   nPrint("A message", k);

}

System.out.println(k);

k is not defined outside the block. So, the program has a compile error

28
New cards

Suppose your method does not return any value, which of the following keywords can be used as a return type?

void

29
New cards

Does the return statement in the following method cause compile errors?

public static void main(String[] args) {

   int max = 0;

   if (max != 0)

      System.out.println(max);

   else

     return;

}

No

30
New cards

Which of the following is the best for generating random integer 0 or 1?

(int)(Math.random() + 0.5)

31
New cards

When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

pass by value

32
New cards

The signature of a method consists of ____________.

method name and parameter list

33
New cards

Does the method call in the following method cause compile errors?

public static void main(String[] args) {

   Math.pow(2, 4);

}

No

34
New cards

All Java applications must have a method __________.

public static void main(String[] args)

35
New cards

Consider the following incomplete code:

public class Test {

   public static void main(String[] args) {

     System.out.println(f(5));

   }

   public static int f(int number) {

     // Missing body

   }

}

The missing method body should be ________.

return number;

36
New cards

Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

a stack

37
New cards

Which of the following should be defined as a void method?

Write a method that prints integers from 1 to 100.

38
New cards

When you pass an array to a method, the method receives __________.

the reference of the array

39
New cards

How many elements are in array double[] list = new double[5]?

5

40
New cards

What is the output of the following code?

double[] myList = {1, 5, 5, 5, 5, 1};

double max = myList[0];

int indexOfMax = 0;

for (int i = 1; i < myList.length; i++) {

   if (myList[i] > max) {

     max = myList[i];

      indexOfMax = i;

   }

}

System.out.println(indexOfMax);

1

41
New cards

When you return an array from a method, the method returns __________.

the reference of the array

42
New cards

What is output of the following code:

public class Test {

   public static void main(String[] args) {

   int[] x = {120, 200, 016};

   for (int i = 0; i < x.length; i++)

     System.out.print(x[i] + " ");

   }

}

120 200 14

43
New cards

The __________ method copies the sourceArray to the targetArray.

System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

44
New cards

The __________ method sorts the array scores of the double[] type.

java.util.Arrays.sort(scores)

45
New cards

Analyze the following code:

public class Test {

   public static void main(String[] args) {

     int[] a = new int[4];

     a[1] = 1;

     a = new int[2];

     System.out.println("a[1] is " + a[1]);

   }

}

The program displays a[1] is 0.

46
New cards

Suppose a method p has the following heading:

public static int[] p()

What return statement may be used in p()?

return new int[]{1, 2, 3};

47
New cards

What is the representation of the third element in an array called a?

a[2]

48
New cards

The JVM stores the array in an area of memory, called _______, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.

heap

49
New cards

Analyze the following code:

public class Test {

   public static void main(String[] args) {

     int[] x = {1, 2, 3, 4};

     int[] y = x;

     x = new int[2];

     for (int i = 0; i < y.length; i++)

        System.out.print(y[i] + " ");

   }

}


The program displays 1 2 3 4

50
New cards

In the following code, what is the output for list2?

public class Test {

   public static void main(String[] args) {

     int[] list1 = {1, 2, 3};

     int[] list2 = {1, 2, 3};

     list2 = list1;

     list1[0] = 0; list1[1] = 1; list2[2] = 2;

     for (int i = 0; i < list2.length; i++)

        System.out.print(list2[i] + " ");

   }

}


0 1 2

51
New cards

Analyze the following code:

public class Test {

   public static void main(String[] args) {

     double[] x = {2.5, 3, 4};

     for (double value: x)

        System.out.print(value + " ");

   }

}

The program displays 2.5 3.0 4.0

52
New cards

What is output of the following code:

public class Test {

   public static void main(String[] args) {

     int list[] = {1, 2, 3, 4, 5, 6};

     for (int i = 1; i < list.length; i++)

        list[i] = list[i - 1];

     for (int i = 0; i < list.length; i++)

        System.out.print(list[i] + " ");

   }

}

1 1 1 1 1 1

53
New cards

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________.

2.0

54
New cards

The reverse method is defined in this section. What is list1 after executing the following statements?

int[] list1 = {1, 2, 3, 4, 5, 6};

int[] list2 = reverse(list1);

list1 is 1 2 3 4 5 6

55
New cards

The reverse method is defined in the textbook. What is list1 after executing the following statements?

int[] list1 = {1, 2, 3, 4, 5, 6};

list1 = reverse(list1);

list1 is 6 5 4 3 2 1

56
New cards

Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?

[1, 20, 30, 40, 50]

57
New cards

Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?

2

58
New cards

What is the output of the following code?

int[] myList = {1, 2, 3, 4, 5, 6};

for (int i = myList.length - 2; i >= 0; i--) {

   myList[i + 1] = myList[i];

}

for (int e: myList)

   System.out.print(e + " ");

1 1 2 3 4 5

59
New cards

In the following code, what is the output for list1?

public class Test {

   public static void main(String[] args) {

     int[] list1 = {1, 2, 3};

     int[] list2 = {1, 2, 3};

     list2 = list1;

     list1[0] = 0; list1[1] = 1; list2[2] = 2;

     for (int i = 0; i < list1.length; i++)

        System.out.print(list1[i] + " ");

   }

}


0 1 2

60
New cards

If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is __________.

3

61
New cards

What is the correct term for numbers[99]?

indexed variable

62
New cards

Assume int[] t = {1, 2, 3, 4}. What is t.length?

4

63
New cards

Suppose the xMethod() is invoked from a main method in a class as follows, xMethod() is _________ in the class.

public static void main(String[] args) {

   xMethod();

}

a static method

64
New cards

Variables that are shared by every instances of a class are __________.

class variables

65
New cards

An object is an instance of a __________.

class

66
New cards

A method that is associated with an individual object is called __________.

an instance method

67
New cards

________ is invoked to create an object.

A constructor

68
New cards

To prevent a class from being instantiated, _____________________

use the private modifier on the constructor.

69
New cards

To declare a constant MAX_LENGTH as a member of the class, you write

final static double MAX_LENGTH = 99.98;

70
New cards

You can declare two variables with the same name in __________.

different methods in a class

71
New cards

_______ is a construct that defines objects of the same type.

A class

72
New cards

The default value for data field of a boolean type, numeric type, object type is ___________, respectively.

false, 0, null

73
New cards

Suppose the xMethod() is invoked in the following constructor in a class, xMethod() is _________ in the class.

public MyClass() {

   xMethod();

}


an instance method

74
New cards

Given the declaration Circle x = new Circle(), which of the following statement is most accurate.

x contains a reference to a Circle object.

75
New cards

_________ represents an entity in the real world that can be distinctly identified.

An object

76
New cards

The keyword __________ is required to declare a class.

class

77
New cards

Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate?

x contains a reference to an array and each element in the array can hold a reference to a Circle object.