Docsity

Java Programming EXAM 2 Questions & Answers, Exams of Java Programming

Toggle sidebar
bg1
Java Programming EXAM 2 Questions & Answers
100% ACCURATE!!
CH 6 - ANSWER
A structure that allows repeated execution of a block of statements is a ___________. - ANSWERloop
A loop that never ends is a(n) ___________ loop - ANSWERinfinite
To construct a loop that works correctly, you should initialize a loop control ___________. -
ANSWERvariable
What is the output of the following code?
b = 1;
while(b < 4)
System.out.print(b + " "); - ANSWER1 1 1 1 1 1...
Why? - ANSWERWhen b is 1, the comparison in the while statement is true, so 1 prints. The
comparison is made
again, it is still true (because b is still 1), and 1 prints again. The value 1 prints infinitely
because b is never altered.
What is the output of the following code?
b = 1;
while(b < 4)
{
System.out.print(b + " ");
b = b + 1;
} - ANSWER1 2 3
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Java Programming EXAM 2 Questions & Answers and more Exams Java Programming in PDF only on Docsity!

Java Programming EXAM 2 Questions & Answers

100% ACCURATE!!

CH 6 - ANSWER

A structure that allows repeated execution of a block of statements is a ___________. - ANSWERloop A loop that never ends is a(n) ___________ loop - ANSWERinfinite To construct a loop that works correctly, you should initialize a loop control ___________. - ANSWERvariable What is the output of the following code? b = 1; while(b < 4) System.out.print(b + " "); - ANSWER1 1 1 1 1 1... Why? - ANSWERWhen b is 1, the comparison in the while statement is true, so 1 prints. The comparison is made again, it is still true (because b is still 1), and 1 prints again. The value 1 prints infinitely because b is never altered. What is the output of the following code? b = 1; while(b < 4) { System.out.print(b + " "); b = b + 1; } - ANSWER1 2 3

Why?? - ANSWERWhen b is 1, the comparison in the while statement is true, so 1 is output. Then b becomes 2, the Boolean evaluation is still true, and 2 is output. The b becomes 3, the Boolean evaluation is true and 3 is output. Then b becomes 4, the Boolean expression is false, and the loop ends. What is the output of the following code? e = 1; while(e < 4); System.out.print(e + " "); - ANSWERnothing WHYY - ANSWERThe semicolon at the end of the second line creates an empty loop body. The value of e remains 1 forever; it keeps being compared to 4 infinitely If total = 100 and amt = 200, then after the statement total += amt, ___________. - ANSWERtotal is equal to 300 whyyyy - ANSWERThe statement total += amt is equivalent to total = total + amt The prefix ++ is a ___________ operator. - ANSWERa. unary If g = 5, then after h = ++g, the value of h is ___________. - ANSWER If m = 9, then after n = m++, the value of m is ___________. - ANSWER why? - ANSWERThe variable n receives the value of m prior to incrementing If j = 5 and k = 6, then the value of j++ == k is ___________. - ANSWERfalse whyyyyyyyyyy - ANSWERThe value of j++ is 5, so it does not equal k. After this statement executes, j will be 6, but that will be after the value of this comparison has been determined.

and the loop ends. What does the following program segment output? for(f = 0; f < 3; ++f) for(g = 0; g < 2; ++g) System.out.print(f + " " + g + " "); - ANSWER0 0 0 1 1 0 1 1 2 0 2 1 wHYY - ANSWERFirst f is set to 0. Because it is less than 3, the second for loop starts. In the inner loop, g is 0, g < 2 is true, and f and g are output (0 0). Then g is increased to 1; g < 2 is still true and f and g are output (0 1). Then g is 2 and the inner loop ends. In the outer loop, f becomes 1. It is less than 3, and so the inner loop starts over, displaying 1 0 and 1 1. Then when f is 2, 2 0 and 2 1 display. What does the following program segment output? for(m = 0; m < 4; ++m); for(n = 0; n < 2; ++n); System.out.print(m + " " + n + " "); - ANSWER Whhhyyyy - ANSWERIn the first line, m is 0 and the comparison is true. However, because of the semicolon, the loop body is empty and m becomes 2, then 3, then 4. Then the second line executes. Again, it contains an empty loop because of the semicolon, so n is 0, then 1, then 2. Finally, the third line executes, displaying 4 and

CH 7 - ANSWER A sequence of characters enclosed within double quotation marks is a ___________. - ANSWERLiteral String To create a String object, you can use the keyword ___________ before the constructor call,

but you are not required to use this format. - ANSWERnew A String variable name is a ___________. - ANSWERreference The term that programmers use to describe objects that cannot be changed is ___________. - ANSWERimmutable Suppose that you declare two String objects as: String word1 = new String("happy"); String word2; When you ask a user to enter a value for word2, if the user types "happy", the value of word1 == word2 is ___________. - ANSWERfalse WhhY - ANSWERWhen you use == with Strings, you compare their memory addresses, not their contents. he value of word1.equals(word2) is ___________. - ANSWERtrue WWHy - ANSWERThe equals() method returns true when the string contents are identical, including case The method that determines whether two String objects are equivalent, regardless of case, is ___________. - ANSWERequalsIgnoreCase() If a String is declared as: String aStr = new String("lima bean"); then aStr.equals("Lima Bean") is ___________. - ANSWERfalse WhyYy - ANSWERThe equals() method returns true when the string contents are identical, including case If you create two String objects:

The method parseInt() converts a(n) ___________. - ANSWERString to an integer The difference between int and Integer is ___________. - ANSWERa. int is a primitive type; Integer is a class For an alternative to the String class, and so that you can change a String's contents, you can use ___________. – ANSWERStringBuilder An advantage to using an enumerated data type is _____. - ANSWERa. errors are reduced because only a limited set of values can be used with the type CH 10 - ANSWER A way to discover which of two classes is the base class and which is the subclass is to ___________. - ANSWERtry saying the two class names together Employing inheritance reduces errors because ___________. - ANSWERmany of the methods you need have already been used and tested A base class can also be called a ___________. - ANSWERsuperclass Which of the following choices is the best example of a parent class/child class relationship? - ANSWERDog/Poodle The Java keyword that creates inheritance is ___________. - ANSWERextends A class named Building has a public, nonstatic method named getFloors(). If School is a child class of Building, and modelHigh is an object of type School, which of the following statements is valid? - ANSWERmodelHigh.getFloors(); Which of the following statements is true? - ANSWERA child class inherits from a parent class. When a subclass method has the same name and argument types as a superclass method, the

subclass method ___________ the superclass method. - ANSWERoverrides When you instantiate an object that is a member of a subclass, the ___________ constructor executes first - ANSWERparent class The keyword super always refers to the ___________ of the class in which you use it - ANSWERparent class Unlike when you create a String, when you create a StringBuilder, you must use the keyword ___________. - ANSWERnew CH 8 - ANSWER An array is a list of data items that _____________. - ANSWERa. all have the same type When you declare an array, _____________. - ANSWERyou might reserve memory for it in the same statement You reserve memory locations for an array when you _____________. - ANSWERuse the keyword new For how many integers does the following statement reserve room? int[] value = new int[34]; - ANSWER Which of the following can be used as an array subscript? - ANSWERint If you declare an array as follows, how do you indicate the final element of the array? int[] num = new int[6]; - ANSWERnum[5]

  1. If you declare an integer array as follows, what is the value of num[2]? int[] num = {101, 202, 303, 404, 505, 606}; - ANSWER

When you pass an array to a method, the method receives _____________. - ANSWERthe address of the array If a method should return an array to its calling method _____. - ANSWERthe return type in the method header is followed by square brackets CH 9 - ANSWER When you place objects in order beginning with the object with the highest value, you are sorting in _____________ order - ANSWERdescending Using a bubble sort involves _____________. - ANSWERcomparing each array element to the adjacent array element When you use a bubble sort to perform an ascending sort, after the first pass through an array, the largest value is _____. - ANSWERat the end of the list When you use a bubble sort to perform an ascending sort, after the first pass through an array, the smallest value is _____. - ANSWERIt is impossible to determine the answer without more information When array elements are objects, you usually want to sort based on a particular _____________ of the object. - ANSWERa. field The following defines a _____________ array: int[][] nums={ {1, 2}, {3, 4}, {5, 6} }; - ANSWERtwo-dimensional How many rows are contained in the following array? double[][] prices = { {2.56, 3.57, 4.58, 5.59}, {12.35, 13.35, 14.35, 15.00} }; - ANSWER How many columns are contained in the following array?

double[][] prices = { {2.56, 3.57, 4.58, 5.59}, {12.35, 13.35, 14.35, 15.00} }; - ANSWER In the following array, what is the value of code[2][1]? char[][] code = { {'A', 'D', 'M'}, {'P', 'R', 'S'}, {'U', 'V', 'Z'} }; - ANSWER'V' In the following array, what is the value of address[1][1]? String address = { {"123 Oak ", "345 Elm "}, {"87 Maple ", "901 Linden "} }; - ANSWER"901 Linden " In the following array, what is the value of fees.length? double[][] fees = { {3.00, 3.50, 4.00, 5.00}, {6.35, 7.35, 8.35, 9.00} }; - ANSWER In the following array, what is the value of fees[1].length? double[][] fees = { {3.00, 3.50, 4.00, 5.00}, {6.35, 7.35, 8.35, 9.00} }; - ANSWER You place ____ after the data type in the parameter list of a method that receives a twodimensional array - ANSWERtwo pairs of empty brackets A ____ array has rows of different lengths. - ANSWERa. ragged If the value of credits[0].length is not equal to credits[1].length, you know credits is _____. - ANSWERa ragged array Which of the following is a requirement when you use a binary search method with an array? - ANSWERThe array must have been sorted in ascending order

You use a ___________ method access specifier when you create methods for which you want to prevent overriding in extended classes. - ANSWERfinal A compiler can decide to ___________ a final method—that is, determine the code of the method call when the program is compiled. - ANSWERinline

. When a parent class contains a static method, child classes ___________ override it - ANSWERcannot