AP COMP SCI (A)

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following code segment and method. for( int n = 1; n <= 10; n++ ) System.out.print( methodHelix ( n ) + " "); public static int methodHelix ( int n ) { int temp1 = 0; int temp2 = 1; int temp3 = 1; for ( int k = 3; k <= n; k++) { temp3 = temp1 + temp2; temp1 = temp2; temp2 = temp3; } return temp3; } What is printed as a result of executing the code segment?

1 1 1 2 3 5 8 13 21 34

Consider the following code segment. int q = 0; for ( int p = 0; p < 5; p++) { q = q + p; } System.out.println(q); What is printed as a result of executing the code segment?

10

Consider the following method. public static int methodMakeArray( int n ) { int [ ] temp = new int[ n ]; temp[ 0 ] = 4; temp[ 1 ] = 7; for ( int k = 2; k < n; k++) temp[ k ] = temp[ k - 1 ] + temp[ k - 2 ]; return temp[ n - 1 ]; } What value is returned by the call methodMakeArray( 8 )?

123

Consider the following code segment. int x = 0; for ( int y = 1; y <= 12; y++) { while ( x <= y ) { if ( x % 2 == 0 ) x += 2; else y -= 2; } } System.out.println(x); What is printed as a result of executing the code segment?

14

Consider the following method. public static int method190( int p ) { int count = 1; for ( int q = 1; q < p; q++ ) count += count; return count; } What value is returned as a result of the method call method190( 5 )?

16

Consider the following code segment. int [ ] list = { 2, 4, 8, 16, 32, 64, 128, 256 }; for ( int k = 1; k < list.length; k++ ) list[ k ] = list[ k ] / list [ k - 1 ]; for ( int k = 0; k < list.length; k++ ) System.out.println( list [ k ] + " " );

2 2 4 4 8 8 16 16

Consider the following code segment. int x = <some integer greater than zero>; int n = 100; if ( x < 1000 ) { if ( x >= 1000) n = 200; else n = 300; } else { if ( x < 1000) n = 400; else n = 300; } System.out.println( n ); What is printed as a result of executing the code segment?

300

Consider the following code segment and method. int x = 5; x = methodH ( x ); System.out.println( x ); public static int methodH ( int n ) { for ( int k = n; k <= 10; k++) n += k; return n; } What is printed as a result of executing the code segment?

50

Consider the following code segment. int count = 5; for ( int p = 0; p < 7; p++) { if ( p % 3 == 0 ) count--; else count++; } System.out.println(count); What will be printed?

6

Consider the following code segment. int k = 0; for ( k = 1; k <= 5; k++) { k++; } k++; System.out.println( k ); What is printed as a result of executing the code segment?

8

Consider the following method. public static void arrayMethod( int [ ] nums) { int j = 0; int k = nums.length - 1; while ( j < k ) { int x = nums[ j ]; nums[ j ] = nums [ k ]; nums[ k ] = x; j++; k-- } } Which of the following describes what the method arrayMethod( ) does to the array nums?

The contents of the array nums are reversed.

Consider the following code segment and the two classes. List list1 = new List( ); list1.showList( ); public class Person { public Person ( ) { System.out.println("New Person"); } } public class List { private Person [ ] persons; public List ( ) { persons = new Person[5]; } public void showList( ) { for ( Person temp: persons) System.out.print( temp + " " ); } } What is printed as a result of executing the code segment?

null null null null null

The Boolean expression ( A || B ) || ! ( A || B ) evaluates to

true in all cases

The Boolean expression ( A || B ) && A is true

whenever A is true

What values are stored in x and y after the execution of the following program segment? int x = 30; y = 40; if ( x >= 0 ){ if ( x <= 100 ) { y = x * 3; if ( y < 50 ) x /= 10; } else y = x * 2; } else y = -x; Correct Answer

x = 30 y = 90

Which of the following pairs of declarations will cause an error message? 1. double x = 14.7; int y = x; 2. double x = 14.7; int y = (int) x; 3. int x = 14; double y = x;

1 only

The Boolean expression ! ( ( A < B ) || ( C > D ) ) is equivalent to which of the following expressions?

( A >= B ) && ( C <= D )

Consider the following code segment. int [ ] list = { 5, 10, 15, 20, 25, 20, 15, 10, 5 }; int max = list.length - 1; for ( int k = max; k > 0; k-- ) list[ k ] = list[ k ] / list [ max ]; for ( int k = 0; k < list.length; k++ ) System.out.println( list [ k ] + " " );

5 10 15 20 25 20 15 10 1

Consider the following code segment and the List class. List list1 = new List( 8, 5 ); list1.showList( ); public class List { private int [ ] arr; public List ( ) { arr = new int[5]; } public List ( int size, int value) { arr = new int[size]; for ( int i = 0; i < arr.length; i++) arr[ i ] = value; } public void showList( ) { for ( int i = 0; i < arr.length; i++) System.out.print( arr[ i ] + " " ); } } What is the result of executing the code segment

5 5 5 5 5 5 5 5

Consider the following method. public void mystery(int[] data) { for (int k = 0; k < data.length - 1; k++) data[k + 1] = data[k] + data[k + 1]; } The following code segment appears in another method in the same class. int[] values = {5, 2, 1, 3, 8}; mystery(values); for (int v : values) System.out.print(v + " "); System.out.println(); What is printed as a result of executing the code segment?

5 7 8 11 19

Refer to the Date class declared below: public class Date { private int myDay; private int myMonth; private int myYear; public Date( ) { //implementation not shown} public Date(int mo, int day, int yr ) { //implementation not shown} public int month( ) // returns month of Date { //implementation not shown} public int day( ) // returns day of Date { //implementation not shown} public int year( ) // returns year of Date { //implementation not shown} //Returns Date as a String in the form "month/day/year", e.g. 4/3/2002. public String toString( ) { //implementation not shown} } Which of the following correctly constructs a Date object?

Date d = new Date ( 2, 13, 1947 );

public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB=value; } } Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ?

Include the method: public int getC() { return myC; }

Consider the following method. public static String scramble(String word, int howFar) { return word.substring(howFar + 1, word.length()) + word.substring(0, howFar); } What value is returned as a result of the call scramble("compiler", 3)? Correct Answer

ilercom

Consider the following code segment. int q = 0; for (int p = 0; p < 10; p++) { q++; p += q; System.out.print( p + " "); } What is printed as a result of executing the code segment?

1 4 8 13

Refer to the Date class declared below: public class Date { private int myDay; private int myMonth; private int myYear; public Date( ) { //implementation not shown} public Date(int mo, int day, int yr ) { //implementation not shown} public int month( ) // returns month of Date { //implementation not shown} public int day( ) // returns day of Date { //implementation not shown} public int year( ) // returns year of Date { //implementation not shown} //Returns Date as a String in the form "month/day/year", e.g. 4/3/2002. public String toString( ) { //implementation not shown} } A method in a different class has this line: Date d1 = new Date ( myDa, myMo, myYr); The same method tries to create a second Date object d2 that is an exact copy of the d1 object. Which of the following will not do this correctly? 1. Date d2 = d1; 2. Date d2 = new Date ( myDa, myMo, myYr); 3. Date d2 = new Date ( d1.month( ), d1.day( ), d1.year( ) );

1 only

Refer to the Date class declared below: public class Date { private int myDay; private int myMonth; private int myYear; public Date( ) { //implementation not shown} public Date(int mo, int day, int yr ) { //implementation not shown} public int month( ) // returns month of Date { //implementation not shown} public int day( ) // returns day of Date { //implementation not shown} public int year( ) // returns year of Date { //implementation not shown} //Returns Date as a String in the form "month/day/year", e.g. 4/3/2002. public String toString( ) { //implementation not shown} } Consider the implementation of a write( ) method that is added to the Date class: //Print the date in the form m/d/y, for example 2/17/1948 public void write( ) { //implementation code } Which of the following could me used as the //implementation code? 1. System.out.println( myMonth + "/" + myDay +"/" + myYear); 2. System.out.println( month( ) + "/" + day( ) +"/" + year( ) ); 3. System.out.println( this );

1, 2, and 3

Consider the following code segment and method. int x = 10; int y = 20; swap ( x, y ); System.out.println(x + " " + y ); public static void swap( int p, int q ) { int t = p; p = q; q = t; } What is printed as a result of executing the code segment?

20 20

Consider the following method. public void fun( int a, int b ) { a += b; b += a; } What is the output from the following code? int x = 3, y = 5; fun( x, y); System.out.println( x + " " + y )

3 5

Refer to the Date class declared below: public class Date { private int myDay; private int myMonth; private int myYear; public Date( ) { //implementation not shown} public Date(int mo, int day, int yr ) { //implementation not shown} public int month( ) // returns month of Date { //implementation not shown} public int day( ) // returns day of Date { //implementation not shown} public int year( ) // returns year of Date { //implementation not shown} //Returns Date as a String in the form "month/day/year", e.g. 4/3/2002. public String toString( ) { //implementation not shown} } Which of the following will cause an error message? 1. Date d1 = new Date ( 11, 5, 2005 ); Date d2 = d1; 2. Date d1 = null; Date d2 = d1; 3. Date d1 = null; int x = d1.year; Which of the following will cause an error message?

3 only

Refer to the Time class declared below: public class Time { private int myHrs; private int myMins; private int mySecs; public Time( ) { //implementation not show} public Time(int h, int m, int s ) { //implementation not show} //Returns time as a String in the form hours:minutes:seconds. public String toString( ) { //implementation not show} } A client has a display method that prints the time represented by its parameter: //Outputs time t in the form hours:minutes:seconds. public void display ( Time t ) { //method body } Which of the following are correct replacements for //method body? 1. Time T1 = new Time( h, m, s ); System.out.println( T1 ); 2. System.out.println( t.myHrs + ":" + t.myMins + ":" + t.mySecs ); 3. System.out.println( t );

3 only

Consider the following method. public static int methodAPCS ( int n ) { int k1 = 2; int k2 = 3; int k3 = 4; for ( int p = 1; p <= n; p++) { k1 = k2; k2 = k3; k3 = k1 + k2; } return k3; } What is returned as a result of the call methodAPCS( 5 )?

47

Consider the following code segment and method. int [ ] list = { 56, 23, 78, 54, 11, 95, 60, 17, 64}; list = mystery ( list ); for ( int item: list ) System.out.println( item + " " ); public static int [ ] mystery ( int [ ] x ) { int [ ] temp = new int [ x.length ]; int q = temp.length - 1; for ( int p = 0; p < x.length; p++ ) { temp[ q ] = x [ p ]; q--; } return temp; }

64 17 60 95 11 54 78 23 56

Consider the following program. public class APCS { public static void main ( String [ ] args ) { samba ( 65.0);} public static void samba ( int k ) { System.out.println( k ); } public static void samba ( double k ) { System.out.println( k ); } public static void samba ( boolean k ) { System.out.println( k ); } public static void samba ( String k ) { System.out.println( k ); } }

65.0

What output will be produced by this code segment? (Ignore spacing.) for (int i = 5; i >= 1; i--) { for (int j = i; j >= 1; j--) System.out.print ( 2 * j - 1 ); System.out.println( ); }

9 7 5 3 1 7 5 3 1 5 3 1 3 1 1

Consider the following code segment and the two class. List list1 = new List( ); list1.showList( ); public class Person { public Person ( ) { System.out.println("New Person"); } } public class List { private Person [ ] persons; public List ( ) { persons = new Person[5]; for ( int i = 0; i < persons.length; i++ ) persons[ i ] = new Person( ); } public void showList( ) { System.out.println( ); for ( Person temp: persons) System.out.print( temp + " " ); } } What is printed as a result of executing the code segment?

New PersonNew PersonNew PersonNew PersonNew Person Five memory reference outputs like Person@1cd2e5f4

Refer to the following code fragment: double answer = 13 / 5; System.out.println("13 / 5 = " + answer); The output is 13 / 5 = 2.0 The programmer intends the output to be 13 / 5 = 2.6 Which of the following replacement for the first line of code will not fix this problem?

double answer = (double) (13 / 5);

Consider the following code segment. for( int k = 1; k <= 100; k++) if ( ( k % 4 ) == 0 ) System.out.println( k ); Which of the following code segments will produce the same output as the code segment above?

for(int k = 4;k <= 100; k = k + 4) System.out.println(k);

Refer to the Date class declared below: public class Date { private int myDay; private int myMonth; private int myYear; public Date( ) { //implementation not shown} public Date(int mo, int day, int yr ) { //implementation not shown} public int month( ) // returns month of Date { //implementation not shown} public int day( ) // returns day of Date { //implementation not shown} public int year( ) // returns year of Date { //implementation not shown} //Returns Date as a String in the form "month/day/year", e.g. 4/3/2002. public String toString( ) { //implementation not shown} } In a different class (e.g. a Driver class), a client programs a Date object as follows: Date d = new Date( 11, 12, 1970 ); Which of the following subsequent code segments will cause an error?

int t = d.myYear;

Consider the following partial class declaration. public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB=value; } } The following declaration appears in another class. SomeClass obj = new SomeClass(); Which of the following code segments will compile without error?

int x = obj.myA;

Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter. public static int[] append(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int j = 0; j < a1.length; j++) result[j] = a1[j]; for (int k = 0; k < a2.length; k++) result[ /* index */ ] = a2[k]; return result; } Which of the following expressions can be used to replace /* index */ so that append will work as intended?

k + a1.length

Refer to the Time class declared below: public class Time { private int myHrs; private int myMins; private int mySecs; public Time( ) { //implementation not show} public Time(int h, int m, int s ) { //implementation not show} //Returns time as a String in the form hours:minutes:seconds. public String toString( ) { //implementation not show} } Which of the following represents the correct implementation code for the constructor with parameters?

myHrs = h; myMins = m; mySecs = s;

Consider the following method. public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) answer *= n; return answer; } Which of the following represents the value returned as a result of the call compute(n, k) ?

n^k


Conjuntos de estudio relacionados

Fundamentals Nursing Chapter 29 Perioperative Nursing

View Set

- AMT - Airframe - Aircraft Landing Gear Systems

View Set

MKT 355 Marketing in a Digital Age UW- Madison

View Set

Human Resource management Ch 9&10

View Set

ch 10- PPE acquisition and disposition

View Set