Deck 16: RGB Colors, Decimal Conversion, Java Naming, and Random Number Generation

Full screen (f)
exit full mode
Question
In the RGB color system, there are 256 possible values for each color (red, green, and blue). A color is considered gray when its red, green, and blue values are identical. There are __________ shades of gray (excluding black and white).
Use Space or
up arrow
down arrow
to flip the card.
Question
Convert the following numbers to decimal: 0010 1010 (binary) and 2B (hexadecimal).
Question
Give the 4-digit hexadecimal Unicode value for the character '&'. (You can refer to Appendix C.)
Question
In the RGB color system, there are 256 possible values for each color (red, green, and blue). How many possible colors are there?
Question
Write a statement to define each of the following variables using Java naming conventions:
A. A variable named percent that holds the value .023
B. A constant that will hold the number of inches in a foot (12 ).
C. A single character that stores the letter A.
Question
Consider the following source code. What should you name the save file for this source code?
public class SpringBreak
{
// lots of code here
}
Question
Where is the error in the following code sequence, and why? How would you correct it?
float number = 6;
double d = number;
number = 19.7;
Question
Write a Java statement to calculate the value of this formula. Assume that z, x and n are ints and have been assigned values. Assign the result to a variable of the appropriate type.
_____
√ 3z + x
4 - xn
Question
Write Java statements to instantiate a random number generator object and generate a random integer between 100 and 250 (inclusive).
Question
Complete this code in main to perform the requested operations for a date entered by the user in this format: month dd, yyyy.
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in );
System.out.print( "Enter a date > " );
String date = scan.nextLine( );
Output the first letter in the month.
Output the date in all lowercase letters.
Output the year portion of the date-that is, the characters in the date that follow the comma. For example, if the date is February 24, 2016, you would output 2016. (Note: Your code should work for ANY date in that format.)
Question
Convert the String input below to a double and display 5 times that value. Do not hard code either 12.4 or 62.0; assume that you do not know the value of input.
String input = "12.4";
Question
If you want to use the Random class in your program, what statement must you have at the beginning of your program? Import only the Random class.
Question
A String variable named s has been declared and holds some value. Output the number of characters in s.
Question
A String variable named email contains the email of a person in this format: username@serviceProvider.extension. Examples are mike32@yahoo.com and jane21@gmail.com. For simplicity, assume that there is exactly one @ character and one . (dot) character in the String email, and that the @ character is before the . (dot) character.
Output the username, the service provider, and the extension of email.
Question
Complete the code, changing the fill color of the GraphicsContext reference gc to a color with red = 100, green = 200, and blue = 60.
// gc is a GraphicsContext reference
// your code goes here
Question
Complete the code, drawing a line between points (100, 200) and (34, 67).
// gc is a GraphicsContext reference
// your code goes here
Question
Complete the code, drawing a rectangle (with the current color, whatever it is) so that the coordinates of its upper left corner are (100, 200) and the coordinates of its bottom right corner are (175, 225).
// gc is a GraphicsContext reference
// your code goes here
Question
Complete the code, drawing two non-solid concentric circles (with the current fill color, whatever it is) so that their diameters are 200 and 100 and the coordinates of their center are (250, 150).
// gc is a GraphicsContext reference
// your code goes here
Question
Complete the code, drawing three more lines using the current stroke color to complete a square between the points (100, 100) and (300, 300).
// gc is a GraphicsContext reference
gc.strokeLine( 100, 100, 300, 100 );
// your code goes here
Question
Complete the code, drawing a triangle whose edges are blue and whose vertices are the points (100, 125), (150, 200), and (100, 300).
// gc is a GraphicsContext reference
// your code goes here
Question
Complete the code, drawing a solid cyan figure with its vertices at the points (100, 125), (150, 200), (100, 300), and (350, 350); note that the Color class includes a constant for cyan.
// gc is a GraphicsContext reference
// your code goes here
Question
Complete this code. Assume that you have created a budget and have set your maximum daily spending to $30. Assume that the appropriate classes have been imported.
public static void main( String [] args )
{
//***** 1. define the maximum daily budget as a constant
//***** 2. Instantiate (create) a Scanner object.
//***** 3. Input the amount actually spent today.
//***** 4. Using an if/else statement, tell the user how their
// actual spending compares with their maximum daily budget
// (more, less, or the same?)
}
Question
A String variable named s has been declared and holds some value. If its number of characters is even and greater than or equal to 10, output EVEN AND LONG, otherwise, output ODD OR SHORT.
Question
Assume that we have already declared an int named number and a char named letter. We want to assign a boolean value to a variable named result1 such that if number is equal to 100 and letter is equal to A, then result1 is true; otherwise, result1 is false.
boolean result1;
// your code goes here
Question
Assume that we have already declared a String named email. We want to assign a boolean value to a variable named result2 such that if email has 10 characters or more and its first character is A, then result2 is true; otherwise, result2 is false.
boolean result2;
// your code goes here
Question
Write a complete program. Prompt the user for two integers (using Scanner). Assume that the user will enter two well-formatted integers. You do not have to check for that. They should both be greater than or equal to 0 and the first one must be strictly less than the second one; if this is not the case, output WRONG DATA. If that is the case, calculate the exact ratio of the first number divided by the second number (for example, if the numbers are 6 and 10, the ratio is 0.6; if the numbers are 4 and 10, the ratio is 0.4). If that ratio is strictly greater than 0.5, output ABOVE AVERAGE; if it is equal to 0.5 (do not worry about possible rounding errors), output AVERAGE; if it is less than 0.5, output BELOW AVERAGE.
Question
A String variable named s has been declared and holds some value. If s contains the String MIDTERM or FINAL, output EXAM; otherwise, output LECTURE.
Question
Assuming a and b are int variables, apply one of DeMorgan's laws to create an equivalent expression for the following boolean expression:
!( a > 8 && b < 19 )
Question
Write a loop to output the word EXAM 99 times.
Question
Write a loop to calculate the total of all the numbers between 100 and 200 included (i.e., 100 + 101 + … + 199 + 200); output the total.
Question
Inside the main method, prompt the user to enter a word that contains a Z until the user enters one.
Question
Inside the main method, prompt the user to enter a word that has at least five characters until the user does. After that, count how many times the letter A is in that word and output the result.
Example 1:
Enter a word > Helo
Enter a word > BLABLA
Number of As is 2
Example 2:
Enter a word > Hi
Enter a word > Mid
Enter a word > PROGRAMMINGEXAMJAVA
Number of As is 4
Question
The class Ticket has been coded as follows. Write the mutators (setPrice and setService methods) for the Ticket class. The price must be greater than or equal to 0. The service must be either A or B; the default service is B.
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Question
The class Ticket has been coded as follows. Write the accessor (getPrice and getService methods) for the Ticket class.
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Question
The class Ticket has been coded as follows. Code a method that switches the value of service: if it is A, it changes to B; if it is B, it changes to A.
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Question
The class Ticket has been coded as follows. Code the toString method so it returns the service and price separated by a : (colon) as in the following examples:
Example 1: B:34.99
Example 2: A:94.99
Example 3: B:44.99
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Question
The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
What is the data type of the parameter and the return type of the tax method?
public double tax( float rate )
Question
The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
In a client class and inside the main method, myTicket is an object reference of type Ticket. Call the method tax with myTicket, assuming a tax rate of 0.06, and assign the resulting tax value to a variable named myTax.
float taxRate = 0.06f;
// Your code goes here
Question
The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Consider the following constant of class Ticket:
public static char DEFAULT_SERVICE = 'B';
In a client class and inside main, write a statement to output the value of the above constant.
Question
Convert 403 into a binary number, and then into a hexadecimal number.
Question
List three programming languages and an application for each.
Question
What is a LAN, and what is its purpose?
Question
Identify the error in this shortcut operator: a + = 2;
Question
What is the maximum length of an identifier?
Question
How would you determine which integer data type would be best to use?
Question
Why will there be zero iterations of the while loop if the loop condition is false the first time it is evaluated?
Question
Identify whether each of the following problems is a result of a missing priming read or missing update read.
A. The first time the while loop condition is evaluated, the result is unpredictable.
B. When the sentinel value is read and processed, it produces incorrect results.
C. The while loop continuously processes the same data item, leading to an endless loop.
Question
If an int instance variable has an initial value of 10, what can you infer about the constructor for that value?
Question
Place the following steps required to calculate a minimum value in the correct order.
-Assume the first value read is the minimum.
Question
Place the following steps required to calculate a minimum value in the correct order.
-Identify a new current minimum if its value is less than previous minimum.
Question
Place the following steps required to calculate a minimum value in the correct order.
-Compare a value to the current minimum.
Question
Give the values that are assigned to each variable after the statements below are executed.
-int a = 5 / 3; __________
Question
Give the values that are assigned to each variable after the statements below are executed.
-int b = 8 % 3; __________
Question
Give the values that are assigned to each variable after the statements below are executed.
-double c = 3 / 7; __________
Question
Give the values that are assigned to each variable after the statements below are executed.
-double d = 10.0 / 3; __________
Question
Give the values that are assigned to each variable after the statements below are executed.
-int e = 5 + 10 / 3; __________
Question
Give the values that are assigned to each variable after the statements below are executed.
-int f = 3; f += 10; __________
Question
Give the values that are assigned to each variable after the statements below are executed.
-int h = 5 + 10 * 3; _________+
Question
Give the values that are assigned to each variable after the statements below are executed.
-double i = ( double ) ( 6 / 4 ); __________
Question
In the RGB color system, there are 256 possible values for each color (red, green, and blue); a color is considered gray when its red, green, and blue values are identical. How many shades of gray are there (excluding black and white)?
Question
Where is the error in the following code sequence, and why? How would you correct it?
int number = 6;
double d = number;
number = 19.0;
Question
Convert the String input below to an int and display 4 times that value. Do not hard code either 7 or 28; assume that you do not know the value of input.
String input = "7";
Question
Complete the code, drawing a solid circle (with the current fill color, whatever it is) so that its diameter is 200 and the coordinates of its center are (250, 150).
// gc is a GraphicsContext reference
// your code goes here
Question
Assuming a is an int variable and flag is a boolean variable, apply one of DeMorgan's laws to create an equivalent expression for the following boolean expression:
!( a == 100 || flag == false )
Question
A String variable named s has been declared and holds some value. If its number of characters is even, output EVEN; otherwise, output ODD.
Question
Inside the main method, prompt the user to enter a word that does not contain a Y until the user enters one.
Question
Write a loop to output the word "FINAL" 25 times.
Question
Consider the following class:
public class Radio
{
private double frequency;
private String name;
// overloaded constructor
// accessors (get methods)
// mutators (set methods)
// other methods
}
Code the two mutators. Frequency must be between 80 and 110 included. If the parameter is out of that range, set the frequency to 100. There is no constraint on name. The mutators should return the Radio reference that calls them.
// Your code goes here
Question
Consider the following class:
public class Radio
{
private double frequency;
private String name;
// overloaded constructor
// accessors (get methods)
// mutators (set methods)
// other methods
}
Code the overloaded constructor. Call the mutators (set methods) to enforce constraints. Assume that they have been coded correctly.
// Your code goes here
Question
Write the code to compute and output how many times the value 99 is found in an array of integers named numbers.
Question
An array, numbers, has been declared and initialized as follows:
int [ ] numbers = { 45, 78, 99, 12, 66 };
What is the index of 78?
What is the array element at index 4?
Change the array so that we have value 77 instead of 99.
Swap the value at indexes 1 and 2 without knowing what the values are.
Using a loop, assume that you do not know how many elements are in the array and increase all the values of numbers by 10.
Using a loop, assume that you do not know how many elements are in the array and what they are, and calculate and output the number of elements that are strictly greater than 50.
Using a loop, assume that you do not know how many elements are in the array and output every other element of the array, starting at the first element.
Question
An array, letters, has been declared and initialized as follows:
char [ ] letters = { 'A', 'B', 'A', 'C', 'E', 'D' };
What is the index of C?
What is the array element at index 1?
Swap the value at indexes 3 and 5 (C and D) without knowing what the values are. After the swap, the array will look like A B A D E C.
Using a loop, assume that you do not know how many elements are in the array and the contents of the array, and compute and output how many As are in the array letters.
Using a loop, assume that you do not know how many elements are in the array and the contents of the array, and change all the As of letters into Zs; do not change the other letters.
Question
Code a method that returns an int array that is parallel to its array parameter letters. If an element of letters is A, the corresponding element of the returned array is 1; otherwise, it is 0.
Example 1: If the array parameter is A B A, the returned array is 1 0 1.
Example 2: If the array parameter is A B A C D A A, the returned array is 1 0 1 0 0 1 1.
Example 3: If the array parameter is A A E F G, the returned array is 1 1 0 0 0.
public static int [ ] convert( char [ ] letters )
{
// Your code goes here
}
Question
Here is the code for selection sort (for an array of ints):
public static void selectionSort( int [ ] arr )
{
int temp;
int max;
for ( int i = 0; i < arr.length - 1; i++ )
{
max = indexOfLargestElement( arr, arr.length - i );
System.out.println( "max = " + max );
temp = arr[max];
arr[max] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
}
public static int indexOfLargestElement( int [ ] arr, int size )
{
int index = 0;
for ( int i = 1; i < size; i++ )
{
if ( arr[i] > arr[index] )
index = i;
}
return index;
}
We are running the following code:
int [ ] numbers = { 10, 6, 4, 8 };
selectionSort( numbers );
Show what the output statement in the selectionSort method outputs. The question is not what the array looks like at the end; we know it is sorted in ascending order.
Question
Consider the following two-dimensional array:
String [ ][ ] states = { { "CA", "CO" },
{ "MD", "IL", "ME", "MI" }, { "NY", "NJ", "NE" } };
What is the value of states[2][1]?
Retrieve ME from the array states and assign it to a String variable of your choice.
Using a loop, output all the states that start with the letter M in the second row (as in this example).
Assume that you do not know how many columns are in the second row, i.e., do not hard code 4. Assume that you do not know that the three states are MD, ME, and MI.
Question
The variable cities is an ArrayList of type String, Write the code to output all its elements.
Question
The variable numbers is a two-dimensional array of type int. Output all of its elements that are strictly greater than 10.
Question
The ArrayList method trimToSize has the following API:
public void trimToSize( )
It trims the capacity of this ArrayList instance to be the list's current size.
ArrayList numbers = new ArrayList( );
numbers.add( 32 );
numbers.add( 17 );
numbers.add( 6 );
Use trimToSize to trim the capacity of numbers to its current size:
Question
The ArrayList method indexOf has the following API:
public int indexOf( HYPERLINK "http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html" \o "class in java.lang" Object o)
Return the index of the first occurrence of o in this list or -1 if this list does not contain o.
ArrayList cities = new ArrayList( );
cities.add( "Baltimore" );
// more statements adding cities to the ArrayList cities
Use indexOf to retrieve the index of the city New York in cities (it may or may not be there) and assign the result to a variable of your choice.
Question
Class Reply inherits from class Post. Write the header for class Reply (one line only).
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/110
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 16: RGB Colors, Decimal Conversion, Java Naming, and Random Number Generation
1
In the RGB color system, there are 256 possible values for each color (red, green, and blue). A color is considered gray when its red, green, and blue values are identical. There are __________ shades of gray (excluding black and white).
254
2
Convert the following numbers to decimal: 0010 1010 (binary) and 2B (hexadecimal).
0010 1010 = 42 ( = 32 + 8 + 2 ) and 2B = 43 ( = 32 + 11)
3
Give the 4-digit hexadecimal Unicode value for the character '&'. (You can refer to Appendix C.)
0026
4
In the RGB color system, there are 256 possible values for each color (red, green, and blue). How many possible colors are there?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
5
Write a statement to define each of the following variables using Java naming conventions:
A. A variable named percent that holds the value .023
B. A constant that will hold the number of inches in a foot (12 ).
C. A single character that stores the letter A.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
6
Consider the following source code. What should you name the save file for this source code?
public class SpringBreak
{
// lots of code here
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
7
Where is the error in the following code sequence, and why? How would you correct it?
float number = 6;
double d = number;
number = 19.7;
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
8
Write a Java statement to calculate the value of this formula. Assume that z, x and n are ints and have been assigned values. Assign the result to a variable of the appropriate type.
_____
√ 3z + x
4 - xn
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
9
Write Java statements to instantiate a random number generator object and generate a random integer between 100 and 250 (inclusive).
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
10
Complete this code in main to perform the requested operations for a date entered by the user in this format: month dd, yyyy.
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in );
System.out.print( "Enter a date > " );
String date = scan.nextLine( );
Output the first letter in the month.
Output the date in all lowercase letters.
Output the year portion of the date-that is, the characters in the date that follow the comma. For example, if the date is February 24, 2016, you would output 2016. (Note: Your code should work for ANY date in that format.)
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
11
Convert the String input below to a double and display 5 times that value. Do not hard code either 12.4 or 62.0; assume that you do not know the value of input.
String input = "12.4";
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
12
If you want to use the Random class in your program, what statement must you have at the beginning of your program? Import only the Random class.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
13
A String variable named s has been declared and holds some value. Output the number of characters in s.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
14
A String variable named email contains the email of a person in this format: username@serviceProvider.extension. Examples are mike32@yahoo.com and jane21@gmail.com. For simplicity, assume that there is exactly one @ character and one . (dot) character in the String email, and that the @ character is before the . (dot) character.
Output the username, the service provider, and the extension of email.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
15
Complete the code, changing the fill color of the GraphicsContext reference gc to a color with red = 100, green = 200, and blue = 60.
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
16
Complete the code, drawing a line between points (100, 200) and (34, 67).
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
17
Complete the code, drawing a rectangle (with the current color, whatever it is) so that the coordinates of its upper left corner are (100, 200) and the coordinates of its bottom right corner are (175, 225).
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
18
Complete the code, drawing two non-solid concentric circles (with the current fill color, whatever it is) so that their diameters are 200 and 100 and the coordinates of their center are (250, 150).
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
19
Complete the code, drawing three more lines using the current stroke color to complete a square between the points (100, 100) and (300, 300).
// gc is a GraphicsContext reference
gc.strokeLine( 100, 100, 300, 100 );
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
20
Complete the code, drawing a triangle whose edges are blue and whose vertices are the points (100, 125), (150, 200), and (100, 300).
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
21
Complete the code, drawing a solid cyan figure with its vertices at the points (100, 125), (150, 200), (100, 300), and (350, 350); note that the Color class includes a constant for cyan.
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
22
Complete this code. Assume that you have created a budget and have set your maximum daily spending to $30. Assume that the appropriate classes have been imported.
public static void main( String [] args )
{
//***** 1. define the maximum daily budget as a constant
//***** 2. Instantiate (create) a Scanner object.
//***** 3. Input the amount actually spent today.
//***** 4. Using an if/else statement, tell the user how their
// actual spending compares with their maximum daily budget
// (more, less, or the same?)
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
23
A String variable named s has been declared and holds some value. If its number of characters is even and greater than or equal to 10, output EVEN AND LONG, otherwise, output ODD OR SHORT.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
24
Assume that we have already declared an int named number and a char named letter. We want to assign a boolean value to a variable named result1 such that if number is equal to 100 and letter is equal to A, then result1 is true; otherwise, result1 is false.
boolean result1;
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
25
Assume that we have already declared a String named email. We want to assign a boolean value to a variable named result2 such that if email has 10 characters or more and its first character is A, then result2 is true; otherwise, result2 is false.
boolean result2;
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
26
Write a complete program. Prompt the user for two integers (using Scanner). Assume that the user will enter two well-formatted integers. You do not have to check for that. They should both be greater than or equal to 0 and the first one must be strictly less than the second one; if this is not the case, output WRONG DATA. If that is the case, calculate the exact ratio of the first number divided by the second number (for example, if the numbers are 6 and 10, the ratio is 0.6; if the numbers are 4 and 10, the ratio is 0.4). If that ratio is strictly greater than 0.5, output ABOVE AVERAGE; if it is equal to 0.5 (do not worry about possible rounding errors), output AVERAGE; if it is less than 0.5, output BELOW AVERAGE.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
27
A String variable named s has been declared and holds some value. If s contains the String MIDTERM or FINAL, output EXAM; otherwise, output LECTURE.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
28
Assuming a and b are int variables, apply one of DeMorgan's laws to create an equivalent expression for the following boolean expression:
!( a > 8 && b < 19 )
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
29
Write a loop to output the word EXAM 99 times.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
30
Write a loop to calculate the total of all the numbers between 100 and 200 included (i.e., 100 + 101 + … + 199 + 200); output the total.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
31
Inside the main method, prompt the user to enter a word that contains a Z until the user enters one.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
32
Inside the main method, prompt the user to enter a word that has at least five characters until the user does. After that, count how many times the letter A is in that word and output the result.
Example 1:
Enter a word > Helo
Enter a word > BLABLA
Number of As is 2
Example 2:
Enter a word > Hi
Enter a word > Mid
Enter a word > PROGRAMMINGEXAMJAVA
Number of As is 4
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
33
The class Ticket has been coded as follows. Write the mutators (setPrice and setService methods) for the Ticket class. The price must be greater than or equal to 0. The service must be either A or B; the default service is B.
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
34
The class Ticket has been coded as follows. Write the accessor (getPrice and getService methods) for the Ticket class.
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
35
The class Ticket has been coded as follows. Code a method that switches the value of service: if it is A, it changes to B; if it is B, it changes to A.
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
36
The class Ticket has been coded as follows. Code the toString method so it returns the service and price separated by a : (colon) as in the following examples:
Example 1: B:34.99
Example 2: A:94.99
Example 3: B:44.99
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
37
The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
What is the data type of the parameter and the return type of the tax method?
public double tax( float rate )
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
38
The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
In a client class and inside the main method, myTicket is an object reference of type Ticket. Call the method tax with myTicket, assuming a tax rate of 0.06, and assign the resulting tax value to a variable named myTax.
float taxRate = 0.06f;
// Your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
39
The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
Consider the following constant of class Ticket:
public static char DEFAULT_SERVICE = 'B';
In a client class and inside main, write a statement to output the value of the above constant.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
40
Convert 403 into a binary number, and then into a hexadecimal number.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
41
List three programming languages and an application for each.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
42
What is a LAN, and what is its purpose?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
43
Identify the error in this shortcut operator: a + = 2;
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
44
What is the maximum length of an identifier?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
45
How would you determine which integer data type would be best to use?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
46
Why will there be zero iterations of the while loop if the loop condition is false the first time it is evaluated?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
47
Identify whether each of the following problems is a result of a missing priming read or missing update read.
A. The first time the while loop condition is evaluated, the result is unpredictable.
B. When the sentinel value is read and processed, it produces incorrect results.
C. The while loop continuously processes the same data item, leading to an endless loop.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
48
If an int instance variable has an initial value of 10, what can you infer about the constructor for that value?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
49
Place the following steps required to calculate a minimum value in the correct order.
-Assume the first value read is the minimum.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
50
Place the following steps required to calculate a minimum value in the correct order.
-Identify a new current minimum if its value is less than previous minimum.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
51
Place the following steps required to calculate a minimum value in the correct order.
-Compare a value to the current minimum.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
52
Give the values that are assigned to each variable after the statements below are executed.
-int a = 5 / 3; __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
53
Give the values that are assigned to each variable after the statements below are executed.
-int b = 8 % 3; __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
54
Give the values that are assigned to each variable after the statements below are executed.
-double c = 3 / 7; __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
55
Give the values that are assigned to each variable after the statements below are executed.
-double d = 10.0 / 3; __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
56
Give the values that are assigned to each variable after the statements below are executed.
-int e = 5 + 10 / 3; __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
57
Give the values that are assigned to each variable after the statements below are executed.
-int f = 3; f += 10; __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
58
Give the values that are assigned to each variable after the statements below are executed.
-int h = 5 + 10 * 3; _________+
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
59
Give the values that are assigned to each variable after the statements below are executed.
-double i = ( double ) ( 6 / 4 ); __________
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
60
In the RGB color system, there are 256 possible values for each color (red, green, and blue); a color is considered gray when its red, green, and blue values are identical. How many shades of gray are there (excluding black and white)?
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
61
Where is the error in the following code sequence, and why? How would you correct it?
int number = 6;
double d = number;
number = 19.0;
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
62
Convert the String input below to an int and display 4 times that value. Do not hard code either 7 or 28; assume that you do not know the value of input.
String input = "7";
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
63
Complete the code, drawing a solid circle (with the current fill color, whatever it is) so that its diameter is 200 and the coordinates of its center are (250, 150).
// gc is a GraphicsContext reference
// your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
64
Assuming a is an int variable and flag is a boolean variable, apply one of DeMorgan's laws to create an equivalent expression for the following boolean expression:
!( a == 100 || flag == false )
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
65
A String variable named s has been declared and holds some value. If its number of characters is even, output EVEN; otherwise, output ODD.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
66
Inside the main method, prompt the user to enter a word that does not contain a Y until the user enters one.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
67
Write a loop to output the word "FINAL" 25 times.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
68
Consider the following class:
public class Radio
{
private double frequency;
private String name;
// overloaded constructor
// accessors (get methods)
// mutators (set methods)
// other methods
}
Code the two mutators. Frequency must be between 80 and 110 included. If the parameter is out of that range, set the frequency to 100. There is no constraint on name. The mutators should return the Radio reference that calls them.
// Your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
69
Consider the following class:
public class Radio
{
private double frequency;
private String name;
// overloaded constructor
// accessors (get methods)
// mutators (set methods)
// other methods
}
Code the overloaded constructor. Call the mutators (set methods) to enforce constraints. Assume that they have been coded correctly.
// Your code goes here
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
70
Write the code to compute and output how many times the value 99 is found in an array of integers named numbers.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
71
An array, numbers, has been declared and initialized as follows:
int [ ] numbers = { 45, 78, 99, 12, 66 };
What is the index of 78?
What is the array element at index 4?
Change the array so that we have value 77 instead of 99.
Swap the value at indexes 1 and 2 without knowing what the values are.
Using a loop, assume that you do not know how many elements are in the array and increase all the values of numbers by 10.
Using a loop, assume that you do not know how many elements are in the array and what they are, and calculate and output the number of elements that are strictly greater than 50.
Using a loop, assume that you do not know how many elements are in the array and output every other element of the array, starting at the first element.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
72
An array, letters, has been declared and initialized as follows:
char [ ] letters = { 'A', 'B', 'A', 'C', 'E', 'D' };
What is the index of C?
What is the array element at index 1?
Swap the value at indexes 3 and 5 (C and D) without knowing what the values are. After the swap, the array will look like A B A D E C.
Using a loop, assume that you do not know how many elements are in the array and the contents of the array, and compute and output how many As are in the array letters.
Using a loop, assume that you do not know how many elements are in the array and the contents of the array, and change all the As of letters into Zs; do not change the other letters.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
73
Code a method that returns an int array that is parallel to its array parameter letters. If an element of letters is A, the corresponding element of the returned array is 1; otherwise, it is 0.
Example 1: If the array parameter is A B A, the returned array is 1 0 1.
Example 2: If the array parameter is A B A C D A A, the returned array is 1 0 1 0 0 1 1.
Example 3: If the array parameter is A A E F G, the returned array is 1 1 0 0 0.
public static int [ ] convert( char [ ] letters )
{
// Your code goes here
}
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
74
Here is the code for selection sort (for an array of ints):
public static void selectionSort( int [ ] arr )
{
int temp;
int max;
for ( int i = 0; i < arr.length - 1; i++ )
{
max = indexOfLargestElement( arr, arr.length - i );
System.out.println( "max = " + max );
temp = arr[max];
arr[max] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
}
public static int indexOfLargestElement( int [ ] arr, int size )
{
int index = 0;
for ( int i = 1; i < size; i++ )
{
if ( arr[i] > arr[index] )
index = i;
}
return index;
}
We are running the following code:
int [ ] numbers = { 10, 6, 4, 8 };
selectionSort( numbers );
Show what the output statement in the selectionSort method outputs. The question is not what the array looks like at the end; we know it is sorted in ascending order.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
75
Consider the following two-dimensional array:
String [ ][ ] states = { { "CA", "CO" },
{ "MD", "IL", "ME", "MI" }, { "NY", "NJ", "NE" } };
What is the value of states[2][1]?
Retrieve ME from the array states and assign it to a String variable of your choice.
Using a loop, output all the states that start with the letter M in the second row (as in this example).
Assume that you do not know how many columns are in the second row, i.e., do not hard code 4. Assume that you do not know that the three states are MD, ME, and MI.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
76
The variable cities is an ArrayList of type String, Write the code to output all its elements.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
77
The variable numbers is a two-dimensional array of type int. Output all of its elements that are strictly greater than 10.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
78
The ArrayList method trimToSize has the following API:
public void trimToSize( )
It trims the capacity of this ArrayList instance to be the list's current size.
ArrayList numbers = new ArrayList( );
numbers.add( 32 );
numbers.add( 17 );
numbers.add( 6 );
Use trimToSize to trim the capacity of numbers to its current size:
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
79
The ArrayList method indexOf has the following API:
public int indexOf( HYPERLINK "http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html" \o "class in java.lang" Object o)
Return the index of the first occurrence of o in this list or -1 if this list does not contain o.
ArrayList cities = new ArrayList( );
cities.add( "Baltimore" );
// more statements adding cities to the ArrayList cities
Use indexOf to retrieve the index of the city New York in cities (it may or may not be there) and assign the result to a variable of your choice.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
80
Class Reply inherits from class Post. Write the header for class Reply (one line only).
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 110 flashcards in this deck.