Download presentation
Presentation is loading. Please wait.
1
CS302 Week 6 Jim Williams
2
This Week P1 Feedback Midterm Exam 1
Lecture: Arrays, Methods and Review
3
P1 Feedback and grades within next couple of days
May request regrade from Grader for 1 week.
4
Midterm Exam 1 What is the location of the exam? 3650 Humanities
1310 Sterling Hall 272 Bascom Other (due to conflicts)
5
Midterm Exam - Thursday
Bring ID and #2 pencil Exam seating directly in front/behind, 1 empty seat to each side Multiple choice - focus on reading Java Write on your exam any assumptions TA led review session - notes on Piazza
6
What will print out and why?
65 0x42 Error int k = 'A'; System.out.println( k);
7
What is the answer? AP? String s1 = "An important programming tool.";
String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase(); AP? api ANP IM API
8
What is the output? chars
public static void countChar(char [] chars) { if ( chars.length > 0 && chars != null) System.out.println("chars"); else System.out.println("no chars"); } public static void main(String[] args) { char []list = null; countChar( list); chars no chars compile time error runtime error
9
What is output? abcde char [] characters = {'a','b','c','d','e'};
public static void main( String [] args) { char [] characters = {'a','b','c','d','e'}; print( characters); System.out.println( characters); } static void print( char []arr) { arr[2] = 'K'; arr = new char[3]; arr[1] = 'Z’; abcde abKde aZKde error
10
What prints out? name: Fred name: FRED name: FRED1 name: Fred1
static void change( String name) { name.toUpperCase(); name = name + "1"; } public static void main( String [] args) { String name = "Fred"; change( name); System.out.println("name: " + name);
11
What are values of x and y?
static int x = 9; public static void main(String []args) { int y = 10; x = methodA( 15, y); int x = 5; System.out.println("x="+x+ " y="+y); } static int methodA( int y, int x) { y = 1; return x + y; x=5 y=10 x=11 y=1 x=11 y=10 x=5 y=15
12
Midterm Exam Today 5 pm to 7 pm Location:
13
What are values of x and y?
static int x; static int y = 2; static int methodA( int y) { x = y + 1; return x + y; } public static void main(String []args) { int x = 5; y = methodA( x); System.out.println("x="+x+ " y="+y); x=5 y=11 x=3 y=2 x=5 y=2 x=3 y=11
14
What kind of methods? max: class nextInt: instance Math.max( 10, 20);
max: instance nextInt: class Math.max( 10, 20); Random randGen = new Random(); randGen.nextInt( 5); class methods (static) instances methods (non-static)
15
Instance vs. Class (static) Methods
method definition has “static” modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have “static” modifier use instance of class when calling Random randGen = new Random(); randGen.nextInt( 5);
16
What prints out? true char [] chars1 = {'x','y','z'};
false char [] chars1 = {'x','y','z'}; char [] chars2 = {'x','y','z'}; System.out.println( chars1 == chars2 ); System.out.println( chars1.equals( chars2));
17
What prints out? true String []pies = new String[3];
false String []pies = new String[3]; pies[0] = "pizza"; pies[1] = new String("pizza"); pies[2] = "pizza"; System.out.println( pies[1] == pies[2]); System.out.println( pies[1].equals( pies[2]));
18
What is output? 2 3 hello 2 8 line 5 line 3 line 4 line 8 line error
Scanner input = new Scanner( System.in); input.nextLine(); int count = 4; if ( input.hasNextInt()) count = input.nextInt(); else { } System.out.println( input.nextLine()); 3 hello 2 8 line 5 line 3 line 4 line 2 8 line error
19
Where is memory allocated?
name: heap new String("fido"): heap name: stack new String("fido"): stack public static void main( String []args) { String name; new String("fido"); name = new String("spot"); }
20
What is print out? two one two main main two one two two two one main
one two two main static void one() { System.out.print( "one "); } static void two(int num) { System.out.print("two "); one(); public static void main( String []args) { two( 3); System.out.print("main ");
21
What is print out? ling Future lin error
String str = "Falling Off a Cliff " + "by Eileen Dover"; System.out.println( str.substring(3,6)); String str2 = "The Future of Robotics " + " by Cy Borg and Anne Droid"; System.out.println( str2.substring( str2.indexOf('e') + 1, str2.indexOf('o')).trim()); Strings courtesy of Boy Scouts
22
Can methodB change number?
yes no maybe error public static void main( String []args) { double number = 5.0; methodB( number); System.out.println( number); }
23
Can methodB change list, contents?
list: yes array contents: yes list: no array contents: no public static void main( String []args) { double [] list = {5.0,4.3,2.1}; methodB( list ); System.out.println( Arrays.toString(list)); }
24
Can methodB change str, contents?
str: yes the string: yes str: no the string: no public static void main( String []args) { String str = "exam today"; methodB( str ); System.out.println( str ); }
25
What is output? String str; str = 7 + 1 * 2 + "6" + 3 + 1;
72631 964 1464 9631 String str; str = * 2 + "6" ; System.out.println( str);
26
Good Luck on the Exam!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.