CS 165 - Final Quizzes
Select the valid expressions below that concatenate the strings "ABC" and "DEF":
-"ABC"+"DEF" -"ABC".concat("DEF")
Select the valid expressions below that evaluate to true:
-'d' > 'a' -'a' < 'b
Assume that an ArrayList of String objects named arr has been declared and initialized, and the following code is run: arr.add("C"); arr.add(0,"O"); arr.add(2,"L"); arr.remove(0); arr.add(1,"O"); arr.add(1,"R"); arr.remove(1); arr.add(3,"A"); arr.set(2, "D"); arr.set(2, "O"); arr.remove(0); What does the following expression return? (arr.indexOf("C")+arr.indexOf("O"))
-1
Which of the following does inheritance allow for:
-An object of class A will "inherit" the properties of class B if class A extends class B -An object of class B will "inherit" the properties of class A if class B extends class A
Select a non-valid statement
-String str2 = (String) 'a';
Select a valid statement
-String str3 = new String("STRING"); -String str4 = "abc" + 'x' + "def"; -String str5 = "" + 24; -String str1 = "abc";
One difference between LinkedList and arrays in Java is that you can have arrays of ____ types as well as ____, whereas LinkedList can only contain ____.
-primitive -objects -objects
public static long func1(int n) { if (n == 2) return 0; return n * func1(n-1); } What does func1(5) return?
0
Assume the following piece of code: class A{ int att1=1; int att2; public int method1(){ return 0; }} public class B extends A { public int method1(){ return 1; }} Given class A and class B, what would the following lines print? A aObj=new A();System.out.println(aObj.att1);
1
public static long func1(int n) { if (n == 2) return 1; return n * func1(n-1); } What does func1(4) return?
12
A linked list of Strings named list has been declared and initialized. Assume that the following code is run on list: list.add("3"); list.addFirst("1"); list.addLast("2"); list.remove(0); list.addFirst("7"); list.remove("3"); list.addFirst("6"); list.addLast("4"); list.remove(3); list.addFirst("2"); list.addFirst("3"); list.remove("7"); list.addLast("0"); list.addLast("9"); What does list.get(3) return?
2
public static long func2(int n) { if (n == 0) return 0; if (n == 1) return 1; return func2(n-1) + func2(n-2); } What does func2(3) return?
2
In Java, what does the following expression evaluate to? 1.5 * (7 % 3) + 0.5 * 2
2.5
public static long func2(int n) { if (n == 1) return 0; if (n == 0) return 1; return func2(n-1) + func2(n-2); } What does func2(5) return?
3
public static void func4(int n) { System.out.print(n + " "); if (n == 1) return; if (n % 2 == 0) func4(n / 2); else func4(3*n + 1); } What is the output for func4(4)?
4 2 1
What will the following piece of code print? public static int processNumbers(int x,int y,int z){ if(x<=y){ if(x<=z){ return x; } } if(y<=x){ if(y<=z){ return y; } } if(z<=x){ if(z<=y){ return z; } } return x;} public static void main(String[]args){ System.out.println(processNumbers(6,5,7));}
5
A linked list of Strings named list has been declared and initialized. Assume that the following code is run on list: list.add("3"); list.addFirst("1"); list.addLast("2"); list.remove(0); list.addFirst("7"); list.remove("3"); list.addFirst("6"); list.addLast("4"); list.remove(3); list.addFirst("2"); list.addFirst("3"); list.remove("7"); list.addLast("0"); list.addLast("9"); What does list.get(5) return?
9
Assume a class named Car has been declared as follows: public class Car{ String brand, year; public Car(String brand, String year){ this.brand=brand; this.year=year; }} Which of the following is the correct way to declare and initialize an ArrayList of Car objects?
ArrayList<Car> arr=new ArrayList<Car>();
_______ can be implemented recursively
Binary Search
T or F : Assuming list is a LinkedList of String objects that contains the following: [A, B, C, D, E, F]. list.add("G") would at "G" at the first position (0th index) of the linked list.
F
Verda o Flaso: Assume that an ArrayList of String objects named arr has been declared, initialized and is filled with strings. arr.set(2,"ABC"); would insert "ABC" at the third index (counting from zero) of arr.
Falso
Assume a class named Car has been declared as follows: public class Car{ String brand, year; public Car(String brand, String year){ this.brand=brand; this.year=year; } public boolean isOld(){ return Integer.parseInt(this.year)<2000; }} An ArrayList of Car objects named arr has been declared and initialized, and the following code is run: arr.add(new Car("Ford GT","2002"));arr.add(new Car("Chevrolette Corvette","1997"));arr.add(1,new Car("Mercedes CLK-GTR","1996"));arr.set(0,new Car("McLaren","1992"));arr.add(0,new Car("BMW Z8","2007"));arr.set(1,new Car("Ferrari Enzo","2010"));for(Car curr:arr){ if(curr.isOld()){ System.out.println(curr.brand); }} What is the output?
Mercedes CLK-GTRChevrolette Corvette
T or F : One difference between LinkedList and arrays in Java is that you can have arrays of primitive types as well as objects, whereas LinkedList can only contain objects.
T
Verdad o Falso: Assume that an ArrayList of String objects named arr has been declared, initialized and is filled with strings. arr.add(1,"ABC");would add "ABC" at the first index (counting from zero) of arr.
Verdad
Assume that an ArrayList of String objects named arr has been declared and initialized, and the following code is run: arr.add("C"); arr.add(0,"D"); arr.add(2,"E"); arr.remove(0); arr.add(1,"A"); arr.add(1,"B"); arr.remove(1); arr.add(3,"W"); arr.set(2, "Q"); arr.remove(0);
[A, Q, W]
A linked list of Strings named list has been declared and initialized. Assume that the following code is run on list: list.add("A"); list.addFirst("B"); list.addLast("C"); list.remove(0); list.addFirst("D"); list.remove("C"); list.addFirst("E"); list.addLast("F"); list.remove(3); list.addFirst("G"); list.addFirst("H"); list.remove("G"); list.addLast("I"); list.addLast("J"); What does list now contain?
[H, E, D, A, I, J]
A linked list of Strings named list has been declared and initialized. Assume that the following code is run on list: list.add("A"); list.addLast("B"); list.addFirst("C"); list.addFirst("D"); list.remove(2); list.addLast("E"); list.addLast("F"); list.remove(4); list.addLast("G"); list.addFirst("H"); list.remove(2); list.addLast("I"); list.addFirst("J"); What does list now contain?
[J, H, D, B, E, G, I]
Assume that an ArrayList of String objects named arr has been declared, initialized and has the following strings: ["A", "B", "C", "D", "E"]. Which of the following expressions would return a boolean expression depending on whether arr has "C"?
arr.contains("C");
Assuming arr is an ArrayList object, arr.length() returns the number of elements in arr. Whats wrong with this statement?
arr.size() returns the number of elements in arr
Smallest data type
byte
Biggest data type
double
The LinkedList class in Java represents a ______.
doubly linked list
Assume that an ArrayList of String objects named arr has been declared, initialized and is filled with strings. arr.get("ABC");would fetch the index of "ABC" in arr.
false
2nd biggest data type
int
Any recursive method can be implemented using an _____
iterative approach (using loops)
The ArrayList class in Java is located at
java.util.*;
Assume that an LinkedList of String objects named list has been declared, initialized and has the following strings: ["A", "B", "C", "D"]. Which of the following expressions would return a boolean expression depending on whether list has "A"?
list.contains("A")
yes or no : The LinkedList class in Java is located at java.lang.LinkedList.?
no
The recursive call(s) to a method must be located ____ the method body.
outside
Any iterative approach (an approach that uses loops) to solving a problem can also be implemented using a ______ approach.
recursive
2nd smallest data type
short
Array lists are ___ than linked lists for removing elements using remove().
slower
Assuming that a Java program contains a method that calls itself recursively infinitely, running this Java program with a call to the recursive method will lead to a ____.
stack overflow exception
Assuming list is a LinkedList of String objects that contains the following: [A, B, C, D, E, F]. list.length() is a valid method call and returns 6.
this method call isn't valid
What does the following boolean expression evaluate to? (true || false && true)
true
Assume that a Scanner object (sc) has been declared and initialized to read from a file "xyz.txt" as follows: Scanner sc = new Scanner(new File("xyz.txt")); Select the option(s) that will let you read in all the lines from the file and print them:
while(sc.hasNextLine()){System.out.println(sc.nextLine());}