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

arrow
  • Select Tags
search iconSearch Question
  • Select Tags

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 }

(Essay)
4.7/5
(42)

The variable numbers is a two-dimensional array of type int. Output all of its elements that are strictly greater than 10.

(Essay)
4.8/5
(41)

We have coded the following sequence: try { Scanner scan = new Scanner( new File( "data.txt" ) ); System.out.println( "ABC" ); } catch( FileNotFoundException fnfe ) { System.out.println( "DEF" ); } catch( Exception e) { System.out.println( "GHI" ); } It compiles and runs; the output is DEF. What conclusion can you draw from looking at the output?

(Short Answer)
4.9/5
(47)

Consider the following method: public static int foo( String s1, String s2, int i ) { if ( s2.length( ) > s1.length( ) ) return -1; else if ( s1.substring( 0, s2.length( ) ).equals( s2 ) ) return i; else return foo( s1.substring( 1, s1.length( ) ), s2, i + 1 ); } What is the output of the following code? System.out.println( foo( "AB", "XYZ", 0 ) ); What is the output of the following code? System.out.println( foo( "ABC", "A", 0 ) ); What is the output of the following code? System.out.println( foo( "ABCDEFG", "DE", 0 ) ); What does foo( s1, s2, 0 ) return as a function of s1 and s2?

(Essay)
4.8/5
(43)

Give the values that are assigned to each variable after the statements below are executed. -int a = 5 / 3; __________

(Short Answer)
5.0/5
(38)

We have already coded the class User. It includes the instance variables name, age, and email, an overloaded constructor, and the toString method coded as follows: public String toString( ) { // return some String representation of this User object } We are now coding the class SuperUser, which inherits from User. SuperUser has one instance variable, fee, which is a double. We want the toString method of the SuperUser class to return the same String as the toString method of the User class, plus a semicolon, a space, and the value of fee. Example 1: If the toString method of User returns Mike, 23, HYPERLINK "mailto:mike@msn.com" mike@msn.com, and the value of fee is 19.9, the toString method of SuperUser returns Mike, 23, HYPERLINK "mailto:mike@msn.com" mike@msn.com, 19.9. Example 2: If the toString method of User returns Jane, 22, HYPERLINK "mailto:jane@gmail.com" jane@gmail.com, and the value of fee is 24.9, the toString method of SuperUser returns Jane, 22, HYPERLINK "mailto:jane@gmail.com" jane@gmail.com, 24.9. Code the toString method of the SuperUser class here. Assume that we do not know what the toString method of User returns.

(Essay)
4.7/5
(33)

Write Java statements to instantiate a random number generator object and generate a random integer between 100 and 250 (inclusive).

(Essay)
4.9/5
(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 ); } } 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.

(Essay)
4.9/5
(41)

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.

(Short Answer)
4.8/5
(43)

If an int instance variable has an initial value of 10, what can you infer about the constructor for that value?

(Essay)
4.9/5
(41)
Showing 101 - 110 of 110
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)