Docsity

Object Oriented Programming - Exam 1 Questions | COP 3330, Exams of Computer Science

Toggle sidebar
bg1
1) What is the output of the following Program?
public class Q1 {
public static void main(String[] args) {
String a = "hello";
String b = "goodbye";
String c = b;
b = b.concat(a);
System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
}
}
Output
----------
a = hello
b = goodbyehello
c = goodbye
Note: When we execute the line b = b.concat(a), the method
call creates a new String object which stores "goodbyehello"
and then the reference b is set to point to that object. b moves
from previously pointing at the String object storing
"goodbye". However, c is STILL referring to the String object
storing "goodbye".
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object Oriented Programming - Exam 1 Questions | COP 3330 and more Exams Computer Science in PDF only on Docsity!

  1. What is the output of the following Program? public class Q1 { public static void main(String[] args) { String a = "hello"; String b = "goodbye"; String c = b; b = b.concat(a); System.out.println("a = "+a); System.out.println("b = "+b); System.out.println("c = "+c); } } Output

a = hello b = goodbyehello c = goodbye Note: When we execute the line b = b.concat(a), the method call creates a new String object which stores "goodbyehello" and then the reference b is set to point to that object. b moves from previously pointing at the String object storing "goodbye". However, c is STILL referring to the String object storing "goodbye".

  1. What is the output of the following statement? System.out.println(3 + 4 + " = 7"); Output

7 = 7 This statement is read left to right. If both operands to a plus sign are integers, then addition is carried out, thus 3 + 4 evaluates to 7. Then, when we calculate 7 + " = 7", since the second operand is a String, we do String concatenation.

  1. How many copies of the character 'a' are printed out by the following segment of code? String nothing = ""; String temp = "a" for (int i=0; i<5; i++) { nothing = nothing + temp; temp = temp + temp; } System.out.println(nothing); Answer: 31, Temp stores 1 a, then 2 a's, then 4 a's, then 8 a's and then 16 a's. nothing simply stores each of these concatenated together.
  2. What is wrong with the following segment of code? Random r = new Random(); r.nextInt() = 20; Answer: You are not allowed to assign a method to a value.
  1. What is the output of the following segment of code? double a = 2, b = 9, c = 4; double ans = (-b โ€“ Math.sqrt(Math.pow(b, 2) โ€“ 4ac))/(2*a); System.out.println(ans); Answer: -
  2. What is the output of the following code? int[] array1 = new int[3]; array1[0] = 2; array1[1] = 5; array1[2] = 3; int[] array2 = array1; array2[1] = 7; System.out.println(array1[0]+" "+array1[1]+" "+array1[2]); Answer: 2 7 3 Note: array1 and array2 refer to the same array, since only one array is ever created.
  1. What is the output of the following code? int[] array1 = new int[3]; array1[0] = 2; array1[1] = 5; array1[2] = 3; int[] array2 = new int[3]; for (int i=0; i<array2.length; i++) array2[i] = array1[i]; array2[1] = 7; System.out.println(array1[0]+" "+array1[1]+" "+array1[2]); Answer: 2 5 3 Note: Here, two separate arrays are created and the values from the first array are "manually" copied into the second array with the for loop. Thus, when a change is made to array2, it does NOT affect the array that array1 refers to.
  1. What does the following method do? public static int question13(int[] values) { int a = values[0]; for (int i=1; i<values.length; i++) if (values[i] < a) a = values[i]; return a; } Answer: Returns the minimum value stored in values.
  2. In question 13, why is it okay for the for loop to start with i equal to 1 instead of 0? Answer: Because by initializing a to values[0], we've already "looked at" the first value in the array. We can now continue onto the second one.
  1. What is the output of the following program?
  1. What is the output of the following program? public class point { private int x; private int y; public point() { x = 0; y = 0; } public void movex(int step) { x += step; } public void movey(int step) { y += step; } Public toString String() { return "("+x+", "+y+")"; } public static void main(String[] args) { point p = new point(); p.movex(10); p.movey(-5); p.movex(-3); System.out.println(p); } } Output: (7,5)