AP csa FRQ Q1 Past Paper 五年真题汇总 2023-2019

news/2025/3/15 16:21:00/文章来源:https://www.cnblogs.com/BigShuang/p/18773832

Author(wechat): bigshuang2020
ap csa tutor, AP计算机科学a老师
国际教育编程老师, 擅长答疑讲解,带学生实践学习。热爱创作,作品:ap csa原创教案,真题梳理汇总,FRQ专题冲刺。

2023 FRQ Question 1

This question involves the appointmentBook class, which provides methods for students to schedule appointents with their teacher. Appointments can be scheduled during one of eight class periods during the school day, numbered 1 through 8. A requested appointment has a duration, which is the number of minutes the appointment will last. The 60 minutes within a period are numbered 0 through 59. In order for an appointment to be scheduled, the teacher must have a block of consecutive, available minutes that contains at least the requested number of minutes in a requested period. Scheduled appointments must start and end with in the same period.

The AppointmentBook class contains two helper methods, isMinuteFree and reserveBlock. You will write two additional methods of the AppointmentBook class.

public class AppointmentBook {/*** Returns true if the minute in the period is available for an appointment,* and returns false otherwise.* Preconditions: 1 <= period <= 8; 0 <= minute <= 59.*/private boolean isMinuteFree(int period, int minute) {// Implementation not shown}/*** Marks the block of minutes that starts at startMinute in period* and is duration minutes long as reserved for an appointment.* Preconditions: 1 <= period <= 8; 0 <= startMinute <= 59; 1 <= duration <= 60.*/private void reserveBlock(int period, int startMinute, int duration) {// Implementation not shown}/*** Searches for the first block of duration as described in part (a).* during the given period.* returns the first minute in the block if such a block is found * or return -1 if no such block is found.* Preconditions: 1 <= period <= 8; 1 <= duration <= 60.*/public int findFreeBlock(int period, int duration) {// To be implemented in part (a)}/*** Searches periods from startPeriod to endPeriod, inclusive, for a block of* duration free minutes. If such a block is found, calls reserveBlock* to reverse the block of minutes and returns true; otherwise returns false* Preconditions: 1 <= startPeriod <= endPeriod <= 8; 1 <= duration <= 60.*/public boolean makeAppointment(int startPeriod, int endPeriod, int duration) {// To be implemented in part (b)}//There may be instance variables, constructors, and methods that are not shown
}

Part (a):

Write the findFreeBlock method, which searches period for the first block of free minutes that is duration minutes long. If such a block is found, findFreeBlock returns the first minute in the block; otherwise, findFreeBlock returns -1. The findFreeBlock method uses the helper method isMinuteFree, which returns true if a particular minute is available to be included in a new appointment and returns false if the minute is unavailable.

Consider the following list of unavailable and available minutes in period 2:

Minutes in Period 2 Available?
0-9 (10 minutes) No
10-14 (5 minutes) Yes
15-29 (15 minutes) No
30-44 (15 minutes) Yes
45-49 (5 minutes) No
50-59 (10 minutes) Yes

The method call findFreeBlock(2, 15) would return 30 to indicate that a 15-minute block starting with minute \(30\) is available. No steps should be taken as a result of the call to findFreeBlock to mark those \(15\) minutes as unavailable.
The method call findFreeBlock(2, 29) would also return 30. Whenever there are multiple blocks that satisfy the requirement, the earliest starting minute is returned.
The method call findFreeBlock(2, 20) would return -1, since no 20-minute block of available minutes exists in period 2.

Complete method findFreeBlock. You must use isMinuteFree appropriately in order to receive full credit.

/*** Searches for the first block of duration free minutes during period, * as described in part (a). Returns the first minute in the block if such a block * is found or returns -1 if no such block is found.* Preconditions: 1 <= period <= 8; 1 <= duration <= 60.*/
public int findFreeBlock(int period, int duration) {// To be implemented in part (a)
}

Part (b)

Write the makeAppointment method, which searches the periods from startPeriod to endPeriod, inclusive, for the earliest block of duration available minutes in the lowest-numbered period. If such a block is found, the makeAppointment method calls the helper method reserveBlock to mark the minutes in the block as unavailable and returns true. If no such block is found, the makeAppointment method returns false.

Consider the following list of unavailable and available minutes in periods 2, 3, and 4 and three successive calls to makeAppointment:

Period Minutes Available?
2 0-24 (25 minutes) No
2 25-29 (5 minutes) Yes
2 30-59 (30 minutes) No
3 0-14 (15 minutes) Yes
3 15-40 (26 minutes) No
3 41-59 (19 minutes) Yes
4 0-4 (5 minutes) No
4 5-29 (25 minutes) Yes
4 30-43 (14 minutes) No
4 44-59 (16 minutes) Yes
  • The method call makeAppointment(2, 4, 22) returns true and results in the minutes \(5\) through \(26\), inclusive, in period 4 being marked as unavailable.
  • The method call makeAppointment(3, 4, 3) returns true and results in the minutes \(0\) through \(2\), inclusive, in period 3 being marked as unavailable.
  • The method call makeAppointment(2, 4, 30) returns false, since there is no block of \(30\) available minutes in periods 2, 3, or 4.

The following shows the updated list of unavailable and available minutes in periods 2, 3, and 4 after the three example method calls are complete:

Period Minutes Available?
2 0-24 (25 minutes) No
2 25-29 (5 minutes) Yes
2 30-59 (30 minutes) No
3 0-2 (3 minutes) No
3 3-14 (12 minutes) Yes
3 15-40 (26 minutes) No
3 41-59 (19 minutes) Yes
4 0-26 (27 minutes) No
4 27-29 (3 minutes) Yes
4 30-43 (14 minutes) No
4 44-59 (16 minutes) Yes

Complete method makeAppointment. Assume that findFreeBlock works as intended, regardless of what you wrote in part (a). You must use findFreeBlock and reserveBlock appropriately in order to receive full credit.

/*** Searches periods from startPeriod to endPeriod, inclusive, for a block* of duration free minutes, as described in part (b). If such a block is found,* calls reserveBlock to reserve the block of minutes and returns true;* otherwise returns false.* Preconditions: 1 <= startPeriod <= endPeriod <= 8; 1 <= duration <= 60.*/
public boolean makeAppointment(int startPeriod, int endPeriod, int duration) {// To be implemented in part (b)
}

2022 FRQ Question 1

This question involves simulation of the play and scoring of a single-player video game. In the game, a player attempts to complete three levels. A level in the game is represented by the Level class.

public class Level {/** * Returns true if the player reached the goal on this level * and returns false otherwise */public boolean goalReached() {/* implementation not shown */}/** * Returns the number of points (a positive integer) recorded for this level */public int getPoints() {/* implementation not shown */}
}
// There may be instance variables, constructors, and methods that are not shown.

Play of the game is represented by the Game class. You will write two methods of the Game class.

public class Game {private Level levelOne;private Level levelTwo;private Level levelThree;/** Postcondition: All instance variables have been initialized. */public Game() {/* implementation not shown */}/*** Returns true if this game is a bonus game and returns false otherwise.*/public boolean isBonus() {/* implementation not shown */}/*** Simulates the play of this Game (consisting of three levels) * and updates all relevant game data.*/public void play() {/* implementation not shown */}/*** Returns the score earned in the most recently played game, * as described in part (a).*/public int getScore() {/* to be implemented in part (a) */}/*** Simulates the play of `num` games and returns the highest score earned, * as described in part (b).* Precondition: num > 0*/public int playManyTimes(int num) {/* to be implemented in part (b) */}
}
// There may be instance variables, constructors, and methods that are not shown.

(a) Write the getScore Method

Write the getScore method, which returns the score for the most recently played game. Each game consists of three levels. The score for the game is computed using the following helper methods:

  • The isBonus method of the Game class returns true if this is a bonus game and returns false otherwise.
  • The goalReached method of the Level class returns true if the goal has been reached on a particular level and returns false otherwise.
  • The getPoints method of the Level class returns the number of points recorded on a particular level.

Scoring Rules:

  • Level one points are earned only if the level one goal is reached.
    Level two points are earned only if both the level one and level two goals are reached.
    Level three points are earned only if the goals of all three levels are reached.
  • The score for the game is the sum of the points earned for levels one, two, and three.
  • If the game is a bonus game, the score for the game is tripled.

Example Table

Complete the getScore method:

/** Returns the score earned in the most recently played game, as described in part (a). */
public int getScore()

(b) Write the playManyTimes Method

Write the playManyTimes method, which simulates the play of num games and returns the highest game score earned. For example, if the four plays of the game that are simulated as a result of the method call playManyTimes(4) earn scores of 75, 50, 90, and 20, then the method should return 90.

Play of the game is simulated by calling the helper method play. Note that if play is called only once, followed by multiple consecutive calls to getScore, each call to getScore will return the score earned in the single simulated play of the game.

Complete the playManyTimes method:

/** * Simulates the play of num games and returns the highest score earned, * as described in part (b). * Precondition: num > 0 */
public int playManyTimes(int num) 

Class Information for This Question

public class Level {public boolean goalReached();public int getPoints();
}public class Game {private Level levelOne;private Level levelTwo;private Level levelThree;public Game();public boolean isBonus();public void play();public int getScore();public int playManyTimes(int num);
}

2021 FRQ Q1

This question involves the WordMatch class, which stores a secret string and provides methods that compare other strings to the secret string. You will write two methods in the WordMatch class.

public class WordMatch {/** The secret string. */private String secret;/** Constructs a WordMatch object with the given secret string of lowercase letters. */public WordMatch(String word) {/* implementation not shown */}/** * Returns a score for guess, as described in part (a).* Precondition: 0 < guess.length() <= secret.length()*/public int scoreGuess(String guess) {/* to be implemented in part (a) */}/** * Returns the better of two guesses, as determined by scoreGuess and the rules for a * tie-breaker that are described in part (b).* Precondition: guess1 and guess2 contain all lowercase letters.* guess1 is not the same as guess2.*/public String findBetterGuess(String guess1, String guess2) {/* to be implemented in part (b) */}
}

Part (a) - Implementing scoreGuess

To determine the score to be returned, scoreGuess finds the number of times that guess occurs as a substring of secret and then multiplies that number by the square of the length of guess. Occurrences of guess may overlap within secret.

Assume that the length of guess is less than or equal to the length of secret and that guess is not an empty string.

The following examples show declarations of a WordMatch object. The tables show the outcomes of some possible calls to the scoreGuess method.

Example 1

WordMatch game = new WordMatch("mississippi");
Value of guess Number of Substring Occurrences Score Calculation Return Value
"i" 4 4 * 1 * 1 = 4 4
"iss" 2 2 * 3 * 3 = 18 18
"issipp" 1 1 * 6 * 6 = 36 36
"mississippi" 1 1 * 11 * 11 = 121 121

Example 2

WordMatch game = new WordMatch("aaaabb");
Value of guess Number of Substring Occurrences Score Calculation Return Value
"a" 4 4 * 1 * 1 = 4 4
"aa" 3 3 * 2 * 2 = 12 12
"aaa" 2 2 * 3 * 3 = 18 18
"aabb" 1 1 * 4 * 4 = 16 16
"b" 0 0 * 1 * 1 = 0 0

Part (b) - Implementing findBetterGuess

The findBetterGuess method returns the better guess of its two String parameters, guess1 and guess2. If the scoreGuess method returns different values for guess1 and guess2, then the guess with the higher score is returned. If the scoreGuess method returns the same value for guess1 and guess2, then the alphabetically greater guess is returned.

The following example shows a declaration of a WordMatch object and the outcomes of some possible calls to the scoreGuess and findBetterGuess methods.

WordMatch game = new WordMatch("concatenation");
Method Call Return Value Explanation
game.scoreGuess("ten") 9 1 * 3 * 3 = 9
game.scoreGuess("nation") 36 1 * 6 * 6 = 36
game.findBetterGuess("ten", "nation") "nation" Since scoreGuess returns 36 for "nation" and 9 for "ten", the guess with the greater score, "nation", is returned.
game.scoreGuess("con") 9 1 * 3 * 3 = 9
game.scoreGuess("cat") 9 1 * 3 * 3 = 9
game.findBetterGuess("con", "cat") "con" Since scoreGuess returns 9 for both "con" and "cat", the alphabetically greater guess, "con", is returned.

Class information for this question

public class WordMatch{private String secretpublic wordMatch(String word)public int scoreGuess(String guess)public String fndBetterGuess(String guess1,String guess2)
}

2020-FRQ Q1

A mathematical sequence is an ordered list of numbers. This question involves a sequence called a hailstone sequence. If \(n\) is the value of a term in the sequence, then the following rules are used to find the next term, if one exists:

  • If \(n = 1\), the sequence terminates.
  • If \(n\) is even, then the next term is \(\cfrac{n}{2}\).
  • If \(n\) is odd, then the next term is $ 3n + 1 $.

For this question, assume that when the rules are applied, the sequence will eventually terminate with the term \(n = 1\).

The following are examples of hailstone sequences.

Example 1: \(5, 16, 8, 4, 2, 1\)

  • The first term is 5, so the second term is \(5 \times 3 + 1 = 16\).
  • The second term is 16, so the third term is \(\cfrac{16}{2} = 8\).
  • The third term is 8, so the fourth term is \(\cfrac{8}{2} = 4\).
  • The fourth term is 4, so the fifth term is \(\cfrac{4}{2} = 2\).
  • The fifth term is 2, so the sixth term is \(\cfrac{2}{2} = 1\).

Since the sixth term is 1, the sequence terminates.

Example 2: \(8, 4, 2, 1\)

  • The first term is 8, so the second term is \(\cfrac{8}{2} = 4\).
  • The second term is 4, so the third term is \(\cfrac{4}{2} = 2\).
  • The third term is 2, so the fourth term is \(\cfrac{2}{2} = 1\).

Since the fourth term is 1, the sequence terminates.

The Hailstone class, shown below, is used to represent a hailstone sequence. You will write three methods in the Hailstone class.

public class Hailstone {/** * Returns the length of a hailstone sequence * that starts with n, as described in part (a). * Precondition: n > 0 */ public static int hailstoneLength(int n) { // to be implemented in part (a)} /** * Returns true if the hailstone sequence * that starts with n is considered long * and false otherwise, as described in part (b). * Precondition: n > 0 */ public static boolean isLongSeq(int n) { // to be implemented in part (b)} /** * Returns the proportion of the first n hailstone sequences * that are considered long, as described in part (c). * Precondition: n > 0 */ public static double propLong(int n) { // to be implemented in part (c)}// There may be instance variables, constructors, // and methods not shown.
}

Part a

The length of a hailstone sequence is the number of terms it contains. For example, the hailstone sequence in example 1 \((5, 16, 8, 4, 2, 1)\) has a length of \(6\) and the hailstone sequence in example 2 \((8, 4, 2, 1)\) has a length of \(4\).

Write the method hailstoneLength(int n), which returns the length of the hailstone sequence that starts with n.

Part b

A hailstone sequence is considered long if its length is greater than its starting value. For example, the hailstone sequence in example 1 \((5, 16, 8, 4, 2, 1)\) is considered long because its length (\(6\)) is greater than its starting value (\(5\)). The hailstone sequence in example 2 \((8, 4, 2, 1)\) is not considered long because its length (\(4\)) is less than or equal to its starting value (\(8\)).

Write the method isLongSeq(int n), which returns true if the hailstone sequence starting with n is considered long and returns false otherwise. Assume that hailstoneLength
works as intended, regardless of what you wrote in part (a). You must use hailstoneLength appropriately to receive full credit.

Part c

The method propLong(int n) returns the proportion of long hailstone sequences with starting values between \(1\) and \(n\), inclusive.

Consider the following table, which provides data about the hailstone sequences with starting values between \(1\) and \(10\), inclusive.

Starting Value Terms in the Sequence Length of the Sequence Long?
1 1 1 No
2 2, 1 2 No
3 3, 10, 5, 16, 8, 4, 2, 1 8 Yes
4 4, 2, 1 3 No
5 5, 16, 8, 4, 2, 1 6 Yes
6 6, 3, 10, 5, 16, 8, 4, 2, 1 9 Yes
7 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 17 Yes
8 8, 4, 2, 1 4 No
9 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 20 Yes
10 10, 5, 16, 8, 4, 2, 1 7 No

The method call Hailstone.propLong(10) returns \(0.5\), since \(5\) of the \(10\) hailstone sequences shown in the table are considered long.

Write the propLong method. Assume that hailstoneLength and isLongSeq work as intended, regardless of what you wrote in parts (a) and (b). You must use isLongSeq appropriately to receive full credit.

2019 FRQ Q1

The APCalendar class contains methods used to calculate information about a calendar. You will write two methods of the class.

public class APCalendar {/** Returns true if year is a leap year and false otherwise. */private static boolean isLeapYear(int year) {/* implementation not shown */}/** * Returns the number of leap years between year1 and year2, inclusive. * Precondition: 0 <= year1 <= year2 */public static int numberOfLeapYears(int year1, int year2) {/* to be implemented in part (a) */}/** * Returns the value representing the day of the week for the first day of year, * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.*/private static int firstDayOfYear(int year) {/* implementation not shown */}/** * Returns n, where month, day, and year specify the nth day of the year. * Returns 1 for January 1 (month = 1, day = 1) of any year. * Precondition: The date represented by month, day, year is a valid date. */private static int dayOfYear(int month, int day, int year) {/* implementation not shown */}/** * Returns the value representing the day of the week for the given date (month, day, year), * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. * Precondition: The date represented by month, day, year is a valid date.*/public static int dayOfWeek(int month, int day, int year) {/* to be implemented in part (b) */}// There may be instance variables, constructors, and other methods not shown.
}

Part (a)

Write the static method numberOfLeapYears, which returns the number of leap years between year1 and year2, inclusive. In order to calculate this value, a helper method is provided for you:

  • isLeapYear(year) returns true if year is a leap year and false otherwise.

Complete method numberOfLeapYears below

/** * Returns the number of leap years between year1 and year2, inclusive. * Precondition: 0 <= year1 <= year2 */
public static int numberOfLeapYears(int year1, int year2) {// Implementation goes here
}

Part (b)

Write the static method dayOfWeek, which returns the integer value representing the day of the week for the given date (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.

For example:

  • 2019 began on a Tuesday, so January 5, 2019, was a Saturday.
    dayOfWeek(1, 5, 2019) // returns 6
    
  • January 10, 2019, was a Thursday.
    dayOfWeek(1, 10, 2019) // returns 4
    

Two helper methods are provided:

  • firstDayOfYear(year) returns the integer value representing the day of the week for the first day of year.
    • Example: firstDayOfYear(2019) // returns 2 (Tuesday)
  • dayOfYear(month, day, year) returns n, where month, day, and year specify the nth day of the year.
    • Example: dayOfYear(3, 1, 2017) // returns 60
    • Example: dayOfYear(3, 1, 2016) // returns 61 (leap year)

Complete method dayOfWeek below:

/** * Returns the value representing the day of the week for the given date * (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday. * Precondition: The date represented by month, day, year is a valid date. */
public static int dayOfWeek(int month, int day, int year) {// Implementation goes here
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/899246.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自带弹窗-》删除功能提示

@Entry@Componentstruct AlertDialogPage { build() { Column() { Button(删除) .backgroundColor(Color.Red) .onClick(() => { AlertDialog.show( { title: 删除该记录?, //弹窗标题 message: 删除…

ASE90N25-ASEMI工业电机专用ASE90N25

ASE90N25-ASEMI工业电机专用ASE90N25编辑:ll ASE90N25-ASEMI工业电机专用ASE90N25 型号:ASE90N25 品牌:ASEMI 封装:TO-247 批号:最新 最大漏源电流:90A 漏源击穿电压:250V RDS(ON)Max:26mΩ 引脚数量:3 沟道类型:N沟道MOS管、中低压MOS管 漏电流:ua 特性:N沟道MO…

温度转化和蟒蛇绘制

蟒蛇绘制 import turtle turtle.setup(650,360,600,200) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.pencolor("pink") turtle.seth(-40) for i in range(4): turtle.circle(40,80) turtle.circle(-40,80) turtle.circle(40,80/2) …

Java面向对象编程(OOP)

面向过程&面向对象 面向过程思想:步骤清晰简单,第一步该做什么,第二步该做什么... 适合处理一些较为简单的问题 线性思维面向对象四思想:物以类聚,分类的思维模式,思考问题首要解决问题需要哪些分类,然后对这些分类进行单独思考,最后才对某个分类下的细节进行面向过…

第二章作业

由于一至三题只存了源代码,自己写的没有保存,所以仅以截图展示。四至八题则采取代码和截图一起的方式展示。 2.4 import turtle turtle.setup(650,360,800,200) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.seth(-40) colors = ["blue&…

book_第2章例题

1.温度转化(符号在后) 2.温度转化(符号在前) 3.蟒蛇例题

SvelteKit 最新中文文档教程(3)—— 数据加载

前言 Svelte,一个语法简洁、入门容易,面向未来的前端框架。 从 Svelte 诞生之初,就备受开发者的喜爱,根据统计,从 2019 年到 2024 年,连续 6 年一直是开发者最感兴趣的前端框架 No.1:Svelte 以其独特的编译时优化机制著称,具有轻量级、高性能、易上手等特性,非常适合构…

web57笔记(严格过滤-$和()来构造数字)

<?php/* # -*- coding: utf-8 -*- # @Author: h1xa # @Date: 2020-09-05 20:49:30 # @Last Modified by: h1xa # @Last Modified time: 2020-09-08 01:02:56 # @email: h1xa@ctfer.com # @link: https://ctfer.com */// 还能炫的动吗? //flag in 36.php if(isset($_GE…

Contest3898 - 计科23级算法设计与分析平时作业-01

题目链接 A.DNA Sorting 题面思路 题目意思就是说,如果一个字符串中前面的字符比后面的字符大,那么它的无序度就+1,根据这个给一组字符串从最有序到最无序依次输出。那么明白题目意思之后直接模拟即可。 示例代码 #include<bits/stdc++.h>using namespace std;#define…

web56笔记(甚⾄把数字都给过滤掉了,还有部分的特殊字符)

<?php/* # -*- coding: utf-8 -*- # @Author: Lazzaro # @Date: 2020-09-05 20:49:30 # @Last Modified by: h1xa # @Last Modified time: 2020-09-07 22:02:47 # @email: h1xa@ctfer.com # @link: https://ctfer.com*/// 你们在炫技吗? if(isset($_GET[c])){$c=$_GET…

Odoo17中套件追溯

基于序列号的套件产品追溯 我们知道odoo原生的套件BOM类型并不会产生真正的库存,从而导致了某些情况下我们想要对套件进行追溯的困难性。基于这样的一个背景,我们在欧姆生产解决方案中新增了对套件产品的追溯拓展,下面我们就来看看如何使用吧。 产品设置 假设我们这里一台组…

Linux | 堆内存管理

from pixiv进程的地址空间jyy 进程的地址空间 Linux 堆内存管理深入分析如何查看Linux进程的地址空间? 答:pmap /proc/$PID/maps/proc文件系统 动态内核信息: /proc 是一个虚拟文件系统,主要提供内核和正在运行的进程的信息。它不是存储在磁盘上的真实文件,而是在运行时动…