CS 161 SAMPLE FINAL Fall 2014 1. What are the three parts of a loop? How are those three things organized in a while loop? How are they organized in a for loop? 2. Write the code to declare MyStuff to be a primitive double variable, initialized to 7.17867. 3. What are two visibility modifiers in Java? 4. There are two main kinds of Java programs, what are they? 5. Write the Java code for the following (variable names are in bold): A. Balance is assigned Balance plus Deposit. B. Counter is assigned Counter minus 1. C. Average is assigned Total divided by NumItems. D. Interest is assigned Balance multiplied by intrate. 6. What symbols are used for the following operators in Java? a. logical AND e. logical NOT i. relational NOT EQUAL b. logical OR f. integer division j. relational EQUAL c. assignment g. decrement k. remainder integer division d. increment h. conditional operator l. String concatenation 7. Evaluate the following expressions (as the computer would): a. 1 / 2 c. 3 * 2 e. Math.pow(2,4) b. 1 + 2 3 * 5/ 2 d. 15 % 4 f. Math.sqrt(81) 8. Given the following assignments: int j = 5, k=7; evaluate the following: a. j k b. ( j k) * ( j + k) c. j * ( k % 3) 9. When the following code is run, the user types in: The erroneous ways of Doctor No. What will be stored in each of the variables when the code is done running? Scanner scan = new Scanner (System.in); String Message, Word, nextword, HisWord; System.out.println( Enter a sentence please ); Message = scan.nextline(); Int num = Message.length(); Scanner Tokens = new Scanner(Message); Word = Tokens.next(); Char ans = Message.charAt(6) String myword = Word.toUpperCase(); nextword = Tokens.next(); HisWord= nextword.replace ( e, x );
10. Use the following code: Class LibraryBook { private String Title; private boolean Available; public LibraryBook (String Bookname) { Title = Bookname; Available = true; public void BorrowBook() { Available = false; public void ReturnBook() { Available = true; public void displaystatus() { System.out.print( \n + Title+ is ); If(! Available) System.out.print ( not ); System.out.println( available.\n ); a. What is the name of this class? b. List the names, if any, of the instance variables of this class: c. List the names, if any, of the constructors of this class: d. List the names, if any, of the methods of this class: e. Write the code to declare mybook to be an object of this class, having the title: The Joy of Java Write the code to call BorrowBook() for the object mybook. f. Write the code to declare another object of this class (you choose the name and book title). Then call the method DisplayStatus() for your object. g. Write the code to declare an array, called mylibrary, of 100 objects from this class. The objects are not initialized. 11. Write the number of times each loop repeats it iterative part (body of the loop)_. Assume that the variables index and n have been declared as integer variables. Also, zero, infinite, and unknown are perfectly valid answers: a. d. while (index< 10) for( index=0; index < 10; index++) System.out.println( index) ; System.out.println( index);
b. e. index = 0; n = 10; for( int index = 10; index >0; index++) while (index < n) System.out.println( index); System.out.println(index); index ++; c. f. index= 0; n = 0; index = 10; n =0; while( index <= 10) while( index > n) { n += index; { index --; index ++; System.out.println( index); 12. Admission rate to the local theater are based on age. Write an if/else statement to assign the correct value to the double variable Admit using the following table. Assume the variable age holds a positive integer value. Age Cost Under 12 4.00 12 58 6.50 Over 58 5.00 13. Write a for loop to print the integers from 1 to 100. Make sure the output is readable with 10 integers on a line and having a comma and space after each value. 14. Write a for loop to print your name 20 times, using a separate line for each. 15. Using a while loop, write the code to print the multiples of 5, starting with 5 times 1 and ending with 5 times 10. 16. Suppose yourfortune is an integer variable. Write a segment of Java code that uses a switch statement to print the following responses yourfortune Response 1: You will soon go on a long journey 2: You are wise beyond your years. 3: Today is your lucky day! 4: You will soon be very rich! Any other value Your future is not yet known.
17. Write a method that will print a row of asterisks (*). The number of asterisks printed will be determined by an integer parameter passed to the method. The method is public and does not return a value. 18. Suppose mystars is an object of the class to which the method in #17 belongs. Write a call to the method you wrote for #17 that will print 27 asterisks. 19. Write the lines of code necessary to declare the integer variable MyNum and give it a random value between 15 and 30. 20. Write a while loop to print the whole numbers starting at 50 and ending with 98. Re-write the loop using a for-loop instead. 21. Why is the main() method declared public and static?? 22. What is the difference between formal and actual parameters? 23. Change the following for loop to a while loop: for( int value = 0; value < 23; value ++) System.out.println( Hello + value); 24. Write the code that will print the message Wear a Coat! if the temperature is less than 40 and the wind is more than 15 mph. 25. Why is Java considered a strongly typed language? 26. What is a class and how does an object relate to the class? 27. Using the segment of code below a) Add code to convert the string to a character b) ADD a verification of input loop which will ensure that the value entered by the user is one of the options listed: c) Add a switch statement to print a different message for each type of car. Scanner scan = new Scanner(System.in); String Ans; System.out.println( What kind of car do you drive? ); System.out.prinltn( (F)ord, (C)hevy, (H)onda, (M)azda, (T)oyota, (B)uick: ); Ans = scan.next(); 28. Why does an Applet not need a main() method?
29. Write the code to declare an array of 50 integers, then fill the array with random values between 0 and 150; 30. Given an array of integers called MyScores, how do you determine the number of elements contained in the array? 31. The array Writing101 contains 40 integers, which are test scores. Write a method that will receive the array as a parameter, find the index of the lowest score in the array, and return that index. 32. What is the starting index of an array in Java? 33. Write the code to declare an array of 30 objects of the class Dog. 34. Write the code to declare array of integers, using an initializer list containing: 2,4,6,8,9,11,13. 35. As a programmer, to access the contents of the 5 th element stored in an array called MyArray, we use the code MyArray[4]. How does the computer actually find that element in memory? 36. Where on the screen is the coordinate <0,0>? 37. What are the predefined colors available in the Java Color class? Are more colors available? 38. What is a static method? Give an example of calling a static method. 39. Write the code to draw a rectangle from the point (35,45) that has one side of 65 and one of 75. Fill the rectangle with the color green. 40. What is a component? (Definition & Example) 41. What is an event? (Definition & Example) 42. What is a listener? (Definition & Example) 43. What is a dialog box? List three kinds of dialog boxes available in Java. 44. What is a label? 45. What is a text field? How is it different from a label? How is it different from a dialog box? 46. What is a button component? List several types of button components, their similarities, their differences and the events they generate. 47. What is the definition of a component? What is the definition of a container?
48. What three objects are necessary to create a Graphical User Interface in Java? 49. Provide a definition for each of the three objects you identified in #48. 50. How is a panel different from a frame? What is the default Layout Manager for a panel? 51. Write a method called SumRange that receives two integer parameters: First and Second. If First is greater than Second, the method returns zero. Otherwise it returns the Sum of ALL THE INTEGERS between First and Second, inclusive. 52. Write a method called SumOfEvens that receives two integer parameters, First and Second. If First is larger than Second, the method returns zero. Otherwise it returns the sum of the even integers between (but not including) First and Second. 53. Write a method called evenlydivisible that accepts two integer parameters and returns true if the first parameter is evenly divisible by the second, or visa-versa, and returns false otherwise. Return false if either parameter is zero. 54. Write a method called randominrange that receives two integer parameter, First and Second that define a range of integers. Return a random integer in the range defined by First and Second. 55. Write a method called MaxFive that receives five (5) integer parameters and returns the largest of the five. 56. Write a method called MinSix that receives six (6) integer parameters and returns the smallest of the six. 57. Write a method called Alarm that prints the string Alarm! multiple times, on separate lines. The method should accept an integer parameter that specifies how many times the string is printed. Print an error message if the parameter is less than 1. 58. Write a method called Sum100 that returns the sum of the integers from 1 to 100, inclusive. This method will receive no parameters and the sum. 59. Write the code to declare a new Color object using R,G,B values. 60. Write a method called RandomColor that receives NO parameters, but returns a Color object. The method should randomly generate an integer value for the Red, Green and Blue values of your color. 61. Evaluate the following code (as the computer would): a. 3 / 4 b. 9/2 c. (double) 3 / 4 d. (int) 4.791 * 5 e. 22 % 3 f. 18 / (6 2)+ 14 % 5 * 3