Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

Ap Computer Science Midterm Practice Exam- Java Basics, Arrays Questions With 100% Correct Answers

Rating
-
Sold
-
Pages
5
Grade
A+
Uploaded on
12-05-2025
Written in
2024/2025

Ap Computer Science Midterm Practice Exam- Java Basics, Arrays Questions With 100% Correct Answers Identity - ANSWER differentiates one instance from another (like an id number or location in memory) Class (OOP Definition #2) - ANSWER The code you write Defines an object's "state" (as variables) Defines an object's "behavior" (as methods, i.e. functions) "template" or "mold" for creating new objects Tells Java "how" to create an object Instance (OOP Definition #2) - ANSWER Instance = A single actual object "created" based on the template (the class) - the data+code that is actually stored in memory Maintains own "state" Exhibits own "behavior" Maintains own "identity" Two instances, like two cookies made from the same cookiecutter, are still different Referenced with the new keyword Getter - ANSWER A getter returns, or "gets" an instance to the main code. For example, in the rectangle project the user could set the value to whatever they wanted, and the getter method just returned that to the runner. Known as an accessor Setter - ANSWER Allows a user to set an instance, for example making the height of rect1 200. Known as a mutator Overloaded Constructor - ANSWER You can create an alternate Constructor that automatically sets your instance variables to specific starting values. Instead of making a setter for the values, you can put variables in your constructor that the user enters after the new keyword. String [] arr = new String [10]; how many strings are made? - ANSWER 0, all 10 strings created were set to null, therefore they are not real strings Can an Object or a Primitive Type be in an array? - ANSWER Both How do you declare a new array? - ANSWER type [ ] name = new type [number] or type name [ ] = new type [number] or int [ ] George = {1,2,3,4,} What is the name of a value at specific point in the array? - ANSWER Index nums [2] = 10; nums is the name, the index of this is 2, the value is 10 Enhanced For Loop - ANSWER Alternative for-loop to retrieve each item in an array one at a time - much more like for-loops in Python. Can you reference or declare an array in an enhanced for loop? i.e. (int arrVal : arr) { Sln(arr[arrVal]); } - ANSWER No, you can not. If you just call for printing arr, the for loop will go through all the values of the variable and print them. Overloaded Constructor - ANSWER A constructor that has parameters, set to the instance variables Default Constructor - ANSWER A constructor with no parameters that sets the instance variables to default values. i.e. Given an array of ints, return true if every element is a 1 or a 4. only14([1, 4, 1, 4]) → trueonly14([1, 4, 2, 4]) → falseonly14([1, 1]) → true - ANSWER public boolean only14(int[] nums) { for (int i = 0; i h; i++) { if (nums [i] != 1 && nums [i] != 4) { return false; } } return true; } Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. countEvens([2, 1, 2, 3, 4]) → 3countEvens([2, 2, 0]) → 3countEvens([1, 3, 5]) → 0 - ANSWER public int countEvens(int[] nums) { int number = 0; for (int i = 0; i h; i++) { if (nums [i] % 2 == 0) { number = number + 1; } } return number; } What is true about local variables? l. Local variables can be declared in the body of constructors and methods. ll. Local variables may only be used within the constructor or method and cannot be declared to be public or private. lll. When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable. - ANSWER All of the above are true Default value for a double in an array - ANSWER 0.0 Default value for an int in an array - ANSWER 0 Default value for a boolean in an array - ANSWER false Default value for a String in an array - ANSWER null Is it better to use a for loop or an enhanced for loop to print an array? - ANSWER Enhanced for loop Given an array of ints, return true if the number of 1's is greater than the number of 4's more14([1, 4, 1]) → truemore14([1, 4, 1, 4]) → falsemore14([1, 1]) → true - ANSWER public boolean more14(int[] nums) { int count1 = 0; int count4 = 0; boolean counter = false; for (int i = 0; i h ; i++) {

Show more Read less
Institution
Ap Computer Science
Course
Ap Computer Science

Content preview

Ap Computer Science Midterm Practice Exam- Java
Basics, Arrays Questions With 100% Correct Answers

Identity - ANSWER differentiates one instance from another (like an id number or
location in memory)

Class (OOP Definition #2) - ANSWER The code you write
Defines an object's "state" (as variables)
Defines an object's "behavior" (as methods, i.e. functions)
"template" or "mold" for creating new objects
Tells Java "how" to create an object

Instance (OOP Definition #2) - ANSWER Instance =
A single actual object "created" based on the template (the class) - the data+code that
is actually stored in memory
Maintains own "state"
Exhibits own "behavior"
Maintains own "identity"
Two instances, like two cookies made from the same cookiecutter, are still different
Referenced with the new keyword

Getter - ANSWER A getter returns, or "gets" an instance to the main code. For
example, in the rectangle project the user could set the value to whatever they wanted,
and the getter method just returned that to the runner. Known as an accessor

Setter - ANSWER Allows a user to set an instance, for example making the height of
rect1 200. Known as a mutator

Overloaded Constructor - ANSWER You can create an alternate Constructor that
automatically sets your instance variables to specific starting values. Instead of making
a setter for the values, you can put variables in your constructor that the user enters
after the new keyword.

String [] arr = new String [10];

how many strings are made? - ANSWER 0, all 10 strings created were set to null,
therefore they are not real strings

, Can an Object or a Primitive Type be in an array? - ANSWER Both

How do you declare a new array? - ANSWER type [ ] name = new type [number] or
type name [ ] = new type [number] or
int [ ] George = {1,2,3,4,}

What is the name of a value at specific point in the array? - ANSWER Index
nums [2] = 10;
nums is the name, the index of this is 2, the value is 10

Enhanced For Loop - ANSWER Alternative for-loop to retrieve each item in an array
one at a time - much more like for-loops in Python.

Can you reference or declare an array in an enhanced for loop? i.e.
(int arrVal : arr) {
System.out.println(arr[arrVal]);
} - ANSWER No, you can not. If you just call for printing arr, the for loop will go
through all the values of the variable and print them.

Overloaded Constructor - ANSWER A constructor that has parameters, set to the
instance variables

Default Constructor - ANSWER A constructor with no parameters that sets the instance
variables to default values. i.e.

Given an array of ints, return true if every element is a 1 or a 4.
only14([1, 4, 1, 4]) → trueonly14([1, 4, 2, 4]) → falseonly14([1, 1]) → true - ANSWER
public boolean only14(int[] nums) { for (int i = 0; i < nums.length; i++) { if (nums [i] !=
1 && nums [i] != 4) { return false;
}

}
return true;
}

Return the number of even ints in the given array. Note: the % "mod" operator
computes the remainder, e.g. 5 % 2 is 1.
countEvens([2, 1, 2, 3, 4]) → 3countEvens([2, 2, 0]) → 3countEvens([1, 3, 5]) → 0 -

Written for

Institution
Ap Computer Science
Course
Ap Computer Science

Document information

Uploaded on
May 12, 2025
Number of pages
5
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$9.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
TopGradeGuru Teachme2-tutor
Follow You need to be logged in order to follow users or courses
Sold
15
Member since
1 year
Number of followers
0
Documents
2395
Last sold
2 months ago
GRADEHUB

We provide access to a wide range of professionally curated exams for students and educators. It offers high-quality, up-to-date assessment materials tailored to various subjects and academic levels. With instant downloads and affordable pricing, it\'s the go-to resource for exam preparation and academic success.

1.5

2 reviews

5
0
4
0
3
0
2
1
1
1

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions