









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










1) (3 pts)Which of the following is true about comments? ____ a) They have to be written in English. b) They speed up the execution of the program. c) They must be included in any program that compiles. d) None of the above 2) (3 pts) Which of the following are NOT reserved words in Java? (You may have more than one answer.) ___________________ a) if b) String c) static d) args e) while 3) (5 pts) Java is an interpreted language. Why do interpreted languages generally execute slower than compiled ones? 4) (3 pts) Which of the following is a run time error in Java? ____ a) Missing semicolon b) Null Pointer Exception c) Unknown identifier d) Nontermination Exception e) Type mismatch 5) (5 pts) List three syntax errors you commonly make when you code. Why do you think you make these more frequently than other possible syntax errors?
10) (10 pts) Write a static method that takes in a String word and returns a new String that contains every other letter from word, starting with the first letter. (For example, if you pass "thisisastring" to the method, it should return "tiiatig", and if you pass "finals" to the method, it should return "fnl".) public static String find_Half(String word) { } 11) (5 pts) Consider the following code segment: String x = fin.readLine(); StringTokenizer tok = new StringTokenizer(x); do { String temp = x.nextToken(); if (x == "DONE") break; } while (x.hasMoreTokens()); Assuming that the syntax here is correct, and that fin is a BufferedReader reading from an input file that is open and has remaining text, what possible run-time error could occur if this code was run? How could that error be fixed?
12) (3 pts) Which of the following Java expressions is equivalent to the mathematical expression ecos(ln |2x+7|)? ____ a) Math.exp(Math.cos(Math.log(2x+7))) b) Math.exp(cos(log(abs(2x+7)))) c) Math.exp(Math.cos(Math.log(Math.exp(2x+7)))) d) Math.exp(Math.cos(Math.log(Math.abs(2x+7)))) e) None of the above 13) (3 pts)The instance variables of a class should generally be ____ a) primitive data types b) arrays c) accessible outside the class d) private e) None of the above 14) (5 pts)What are the three visibility modifiers we discussed in class? Give guidelines for when it is appropriate to use each. 15) (5 pts)What is the problem with the code below? public class Test { private int x; private int y; public Test() { x=0; y=0; } public void square() { int x,y; x = xx; y = yy; } }
16) (5 pts) What is the output of the following code? public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public setX(int x) { this.x = x; } public String toString() { return x+" "+y; } public static void main(String[] args) { Point p1 = new Point(3,5); Point p2 = p1; p2.setX(4); System.out.println(p1); System.out.println(p2); } }
17) (3 pts) What is a difference between an interface and an abstract class? ____ a) An abstract class can define methods while an interface can not. b) An object of an abstract class can be instantiated but an object of an interface can not. c) A reference of an abstract class can be used, while a reference to an interface can not. d) An abstract class is useful while an interface is not. e) Choices a and c are correct. f) None of the above. g) All of the above(a through d).
18) (8 pts) Match each Color object with the color it actually represents: a) new Color(255,0,0) 1) Gray ____ b) new Color(0,255,0) 2) Green ____ c) new Color(0,0,255) 3) Blue ____ d) new Color(255,255,255) 4) Yellow ____ e) new Color(0,0,0) 5) White ____ f) new Color(128,128,128) 6) Red ____ g) new Color(255,0,255) 7) Purple ____ h) new Color(255, 255, 0) 8) Black ____ 19) (11 pts)What is the output of the following code? public class A { protected int x; protected char c; public A(int y, char d) { x = y; c = d; } public void increment() { x++; c = (char)(c+1); } public void add(int a) { x+=a; } public void add(char a) { int rotate = (int)(a - 'a'); c = (char)(c+rotate); } public String toString() { return x + " " + c; } } public class B extends A { private int y; private char d; public B(int x1, int y1, char c1, char d1) { super(x1,c1); y = y1; d = d1;
20) (5 pts) What will be drawn by the code in the paint method given below? (You may assume that all other supporting code is working properly.) Place your drawing below. public void paint(Graphics g) { g.drawRect(10,10,50,100); g.clearRect(60,10,2,100); g.drawOval(70,10,50,100); g.drawLine(130,10,130,110); g.drawOval(130,10,50,50); } 21) (10 pts) Write a segment of code to read in an integer from the user. If the user enters a String that is not in the proper format of an integer, reprompt the user to enter again. (Hint: Your code segment must catch a NumberFormatException.)
22) The following program is supposed to be designed to search a Boggle board for all possible combinations of words. A Boggle board is a 4x4 grid of characters. A word is on the board if it can be formed by letters on the board that are connected in a path. For the purposes of this question, a letter on the board may be repeated in a word. So, for example, consider the board below: a h t e n o i y r s p t i e g u The word "honor" is on this board because the h is adjacent to the o, which is adjacent to an n, which is adjacent to the first o, which is adjacent to an r. The word "spy" appears on the board as well. However, the word "hay" does not appear on the board because the a is not adjacent to the y. The program below is designed to create a Boggle board randomly (not perfectly random), and then list all the words that appear on the board that are also in the dictionary. (For the purposes of this program, the dictionary is stored in a file /usr/dict/words. This file contains one word per line, all lower case, in alphabetical order.) Some parts have been written for you while others have been left for you to fill in. Keep in mind that you have to write your code so that it works with what has already been written for you. Also, you will be asked several questions regarding the given code. import java.util.Random; import java.lang.Math; import java.io.*; public class Boggle { private char[][] board; private Random r; final static int freq[] = {8, 10, 13, 17, 30, 32, 34, 40, 47, 48, 49, 53, 55, 62, 69, 71, 71, 77, 83, 92, 94, 95, 97, 97, 99, 100}; public Boggle() { board = new char[4][4]; r = new Random(); for (int i=0; i<16; i++) { int randval = Math.abs(r.nextInt())%100; int index = find_Place(randval); board[i/4][i%4] = (char)('a'+index); }
// This method will list all words in the dictionary file that are // contained in the Boggle board. public void list_All() throws IOException { BufferedReader fin = new BufferedReader(new FileReader("/usr/dict/words")); } public static void main(String[] args) throws IOException { Boggle mine = new Boggle(); mine.print_Board(); mine.list_All(); } } a) (5 pts) Explain how the constructor for the Boggle class works. For a bit of extra credit, can you take a guess as to what strategy is used in creating a Boggle object in this constructor? b) (5 pts) Explain a trace through the execution of search_Word, if the method was called with a parameter that contained no letters that appeared in the Boggle board. c) (5 pts) find_Word_Help is recursive. What condition in a particular find_Word_Help call necessitates a recursive call? How exactly does this method work? (The more specific you are the more credit you'll get.)
d) (5 pts) Write the print_Board method. // Prints out a reasonable representation of the 4x4 board. public void print_Board() { } e) (12 pts) Write the search_Dictionary method. It's requirements are given below, and the dictionary file has already been opened for you. // This method opens the dictionary file and then sees if the String word is contained in // the file. If it is, true is returned, otherwise false is returned. public static boolean search_Dictionary(String word) throws IOException { BufferedReader fin = new BufferedReader(new FileReader("/usr/dict/words")); }
Extra Page - If you would like any work on this page graded, please label your work clearly.