Presentation is loading. Please wait.

Presentation is loading. Please wait.

160 Exam 2 Prep.

Similar presentations


Presentation on theme: "160 Exam 2 Prep."— Presentation transcript:

1 160 Exam 2 Prep

2 How would I initialize an integer array named iArray with 9 elements?
int iArray = new int [9]; int [] iArray = new int [9]; int [] iArray = int [9]; int iArray = 9; B

3 What does the console print based off the following code?
public class Practice { public static void main (String [] args){ String s = “Koala Bears”; for (int i = 0; i < s.length(); i+=2) System.out.print(s.charAt(i)); } KaaBas

4 What would I call this method in the main method?
public class Practice { public static doubleMyInt(int i) { return i * 2; } Practice prac = new Practice(); prac.doubleMyInt(9); doubleMyInt(9); doubleMyInt(); B

5 What would the method declaration (or header) be if I wanted a public, non-static method that takes a String as a parameter, it returns an integer. The method is called “toAscii”. public non-static int toAscii (String s) public String toAscii (int i) public int toAscii (String s) public non-static int toAscii (int i) C

6 What would the method declaration (or header) be if I wanted a public, static method that returns nothing, takes no parameters and is called “method1”. public void method1 (void) public static method1() public method1() public static void method1() D

7 What does the code print?
public class Practice { public static void main (String [] args){ int i = 0; iterator(i); System.out.println(i); } public static int iterator (int i) { return i++;

8 What does the code print?
import java.util.Arrays; public class Practice { public static void main (String [] args){ char [] s = {'a','b','c','d'}; change(s); System.out.println(Arrays.toString(s)); } public static void change(char [] s) { s[1] = ‘z’; [a, z, c, d]

9 Which one of these would not go to the end of String s
Which one of these would not go to the end of String s? (Specify which ones cause errors and which ones are just incorrect (but they compile)? for (int i = 0; i <= s.length(); i++) for (int i = 0; i < s.length()+1; i++) for (int i = 0; i < s.length(); i++) for (int i = 0; i <= s.length() -1; i++) for (int i = 0; i < s.length() -1; i ++) A gives an error (goes one more) B gives an error (goes one more) C is correct D is correct E is incorrect only goes to the second to the last index

10 What would this print? import java.lang.Math; public class Practice { public static void main (String [] args){ Practice prac = new Practice(); double myDouble = 2.0; prac.cubed(myDouble); } public double cubed (double d) { return Math.pow(d, 3); Doesn’t print anything

11 Answers

12 B KaaBas C D [a, z, c, d] A gives an error (goes one more) B gives an error (goes one more) C is correct D is correct E is incorrect only goes to the second to the last index 9. Doesn’t print anything


Download ppt "160 Exam 2 Prep."

Similar presentations


Ads by Google