Comp-sci Final Exam
The String method to get the single character from the String name at position 3 is:
name.charAt(3)
What is output by the code below? public class CS { public void one() { System.out.print("one"); } } //code in the main of another class CS test = new CS(); test.one(); test.one(); test.one(); test.one(); e
oneoneoneone
When writing a method, you must always have an open and close what ?
Curly brace
What is the output by the code below? System.out.print("ab\\ab" );
ab\ab
Given the following statement, var must be defined as which of the following types? var=keyboard.nextInt();
int
Which of the following is a 32 bit data type?
int
Which of the following lines correctly defines an integer variable?
int i =234;
Which of the following is a valid identifier in Java?
java1234
What is the output of this code? String x = "meat"; String y = "owls"; System.out.println(x.substring(0,2) + y.substring(0,2));
meow
Which of the following Scanner methods can be used to read in a String?
next()
Which of the following Scanner methods can be used to read in the value 45 ?
nextByte() nextShort() nextInt() nextDouble()
Which of the following Scanner methods can be used to read in the value 52.245 ?
nextDouble()
What is output by the code below? public class CS { public void one() { System.out.print("one"); two(); } public void two() { System.out.print("two"); } } //code in the main of another class CS test = new CS(); test.one();
onetwo
Which of the following methods would move the cursor down to the next line?
println()
Assume the following code has been ran: String s = "Rock 'n Roll!";
s.lastIndexOf("o") returns _9____ s.indexOf("o") returns _1____ s.toUpperCase() returns ROCK 'N ROLL! s.toLowerCase() returns rock 'n roll s.replaceAll("Rock", "Heck"); returns heck 'n roll!
Assume the following code has been ran. String s = "Fluffy";
s.length is _7____ s is indexed from _0____ to __6___ The character at index 2 is _u____ The character at s.charAt(s.length()-1)) is __y__
Assume the following code has been ran: String s = "Green men from Mars";
s.substring(2, 5) returns _een____ s.substring(15) returns _Mars____ s.substring (6, 8) returns _me____
Which of the following can be used to comment code in Java?
//
What is output by the code below? double x = 4/3; System.out.print(x);
1.0
What is output by the code below? public class Check { private int fun; public void change() { fun = 100; int fun = 99; } public String toString() { return " " + fun; } } //client code Check test = new Check(); test.change(); out.println(test);
100
What is the output? System.out.print(3 + 3 * 3);
12
What is output by the code below? int x = 9, y = 8; int z = x + y; double a = z; System.out.print(a);
17.0
What is output by the code below? public class Check { private int one, two, total; public void setNums(int n1, int n2) { one = n1; two = n2; } public void add() { total = one + two; two = 9; } public String toString() { total = 2; return "" + total; } } //code in the main of another class Check test = new Check(); test.setNums(9,7); test.add(); out.println(test);
2
What is output by the code below? int x = 8; x = x % 3; System.out.print(x);
2
What is output by the code below? System.out.println( Math.floor(6.7) );
6.0
What is the ASCII value of 'B'?
66
What is output by the code below? System.out.println( Math.ceil(6.7) );
7.0
What is output by the code below? int x = 7; x++; System.out.print(x);
8
In Java, every program statement ends with a what ?
;
You should never put a ___________what after an open brace?
;
Which of the following would correctly define and instantiate a Scanner?
Scanner kb = new Scanner(in);
Which of the following lines correctly defines a String variable?
String s = "cdef";
Given a word with no spaces, determine the location of the ? in the word and then create and return a new String that does not contain a ?. Part A - Write method findQuestionMark that will return the index position of the ? in the parameter word. All words with have at most one ?.
The call findQuestionMark( "dogs?andcats" ) would return 4. The call findQuestionMark( "dogsand?cats" ) would return 7. The call findQuestionMark( "?dogsandcats" ) would return 0. The call findQuestionMark( "dogsandcats" ) would return -1. public static int findQuestionMark( String word ) { int index; index = word.indexOf("?"); return index; }
Part B - Write method removeQuestionMark that will return a new String based on the parameter word that does not contain a ?. You must call method findQuestionMark from part A. Assume that part A works as intended regardless of what you wrote in part A.
The call removeQuestionMark( "fred?flintstone" ) would return fredflintstone. The call removeQuestionMark( "howardthe?goose" ) would return howardthegoose. The call removeQuestionMark( "?woweeistheword" ) would return woweeistheword. public static String removeQuestionMark( String word ) { String firstPart, secondPart; int indexRemove; indexRemove = findQuestionMark(word); firstPart = word.substring(0, indexRemove); secondPart = word.substring(indexRemove+1) return firstPart+secondPart; }
Which of the following is the newline character?
\n
What is the output by the code below System.out.print("a\tc" ); ?
a c
What is a method
a tool that helps organize a program
Which of the following lines correctly defines a char variable?
char c = 'a';
Which of the following lines correctly defines a double variable?
double d = 234.1;