Download presentation
Presentation is loading. Please wait.
1
First Semester Review
2
List of Topics Covered OOP Concepts Objects, methods, classes
Documentation Identifiers Method calls Target, method, arguments The Java API String methods Math methods Arithmetic operations +, -, *, / , % Compound assignment/increment-decrement Primitive types Conversion/casting Variables Declaration, assignment, initialization References (objects) v. primitives Arrays Declaring, initializing Accessing elements Traversal Multi-dimensional ArrayList Methods Declaring, calling Parameters Reference Semantics Return values Overloading Loops while, do-while, for Conditionals if, if-else, if-else if-else Relational operators Boolean operators Console input/Scanner Classes Constructors Fields Methods Access (public/private) Constants (final)
3
Primitive Types Which of the following statements displays 1234? (There may be more than one.) System.out.print(12 * ); System.out.print(“12” + 34); System.out.print(12 + “34”); Answer: I, II, and III The + operator defaults to arithmetic addition, unless one of the operands is a string, in which case it is concatenation.
4
Arithmetic Operators Consider the following method:
public static int countEvenDigits(int n) { int count = 0; while (n > 0) { int d = n % 10; if (d % 2 == 0) { count++; } <statement1> return count; What should go in place of <statement1> to make the method work as intended? Answer: n /= 10;
5
Overloading True or false: Overloaded methods may have the same number of parameters? Answer: True As long as the types of the parameters are different, two overloads may have the same number of parameters.
6
Parameter Passing Consider the following method:
public void change(double[] nums) { for (int k = 0; k < nums.length; k++) { nums[k] = 5.4; } What will be stored in samples after the following code is executed? double[] samples = {1.0, 2.1, 3.2, 4.3}; change(samples); Answer: {5.4, 5.4, 5.4, 5.4} The array is passed by value, but changing the elements still modifies the original array.
7
Two-Dimensional Arrays
What should go in the place of <expression1> and <expression2> to make the following correctly populate a two-dimensional array vals with random numbers between 0 and 1. for (int i = 0; i < vals.length; i++) { for (int j = 0; j < <expression1>; j++) { <expression2> = Math.random(); } Answer: vals[i].length and vals[i][j]
8
Boolean Logic What is the value of the following expression when a is false and b is true? !(!a || b) || (!a && b) Answer: true a = true a = false b = true false true b = false
9
Debugging Find a bug in the following method:
public static double volumeOfSphere(int r) { double pi = ; return 4 / 3 * pi * Math.pow(r, 3); } Answer: The expression (4 / 3) will have the value 1 because it is integer division.
10
Random Numbers Write an expression to generate a random integer between 2 and 50 inclusive. Answer: (int)(Math.random() * 49) + 2;
11
Constructors True or false: Constructors’ return type must be declared as void. Answer: False Constructors do not have a declared return type.
12
Console Output What is printed by the following code:
public static void printStuff() { System.out.print(“Your rating: ”); for (int i = 0; i < 3; i++) { System.out.println(“*”); } System.out.println(“\nGood job!”); Answer: Your rating: * * Good job!
13
Loops How many lines of output will this code produce? Answer: 6
for (int n = 50; n > 0; n /= 2) { System.out.println(n); } Answer: 6
14
Loops/Arrays Give a brief description of what the following code does.
// a is an initialized array of integers int c = 0; int[] a2 = new int[a.length]; for (int i = 0; i < a.length; i++) { if (a[i] >= 0) { a2[c] = a[i]; c++; } Answer: Copy all non-negative integers from a to a2.
15
String Methods What is returned when the following method is called with arguments “cat”, “xy”, and 1? public static String insert(String str1, String str2, int pos) { String first = str1.substring(0, pos); String second = str1.substring(pos); return first + str2 + second; } Answer: “cxyat”
16
Arrays What will happen when the following code is executed?
int[] values = {5, 6, 3, 7, 1, 4, 9, 8, 0, 7, 12}; int[] counts = new int[10]; // initialize counts to all zeroes for (int i = 0; i < values.length; i++) { counts[values[i]]++; } Answer: An ArrayIndexOutOfBoundsException will be thrown. 12 is an invalid index for counts
17
Defining Classes Consider the following incomplete class: Answer:
public class Student { private String name; /* more code */ } Write an accessor method for the name variable in the Student class. Answer: public String getName() { return name;
18
Constructors Consider the following class:
Public class Card { private String suit; private int value; public Card(String suit, int value) { ... } } Write an implementation for the constructor. Answer: public Card(String suit, int value) { this.suit = suit; this.value = value;
19
Final Question
20
Final Question Implement the following method:
// Given a square array of ints, determine whether // or not the sums of the integers on the two // diagonals are equal public static boolean areDiagsEqual(int[][] vals)
21
Final Question Implement the following method:
public static boolean areDiagsEqual(int[][] vals) { int diag1 = 0, diag2 = 0; int n = vals.length – 1; for (int i = 0; i < vals.length; i++) { diag1 += vals[i][i]; diag2 += vals[i][n – i]; } return diag1 == diag2;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.