AP Computer Science Practice Exam A
With Correct Solutions 2024
Consider .the .following .method.
public .static .int .mystery(int[] .arr)
{
.int .x .= .0
for .(int .k .= .0; .k .< .arr.length; .k .= .k .+ .2)
.x .= .x .+ .arr[k]
return .x;
}
Assume .that .the .array .nums .has .been .declared .and .initialized .as .follows.
.int[] .nums .= .{3, .6, .1, .0, .1, .4, .2};
(A) .5
(B) .6
(C) .7
(D) .10
(E) .17 .- .correct .answer.(C) .7
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?
(A) .int .x .= .obj.getA();
(B) .int .x;
.obj.getA(x);
(C) .int .x .= .obj.myA
(D) .int .x .= .SomeClass.getA();
(E) .int .x .= .getA(obj); .- .correct .answer.(A) .int .x .= .obj.getA
Which .of .the .following .changes .to .SomeClass .will .allow .other .classes .to .access .but .not
.modify .the .value .of .myC .?
(A) .Make .myC .public.
(B) .public .int .getC()
.{ .return .myC; .}
(C) .private .int .getC()
.{ .return .myC; .}
(D) .public .void .getC(int .x)
(E) .private .void .getC(int .x)
.{ .x .= .myC; .} .- .correct .answer.(B) .public .int .getC()
.{ .return .myC; .}
Consider .the .following .code .segment.
.int .x .= .7;
.int .y .= .3;
.
.if .((x .< .10) .&& .(y .< .0))
.System.out.printIn("Value .is: ." .+ .x .* .y);
.else
.System.out.printIn("Value .is: ." .+ .x ./ .y);
What .is .printed .as .a .result .of .executing .the .code .segment
(A) .Value .is: .21
(B) .Value .is: .2.3333333
(C) .Value .is: .2
(D) .Value .is: .0
(E) .Value .is: .1 .- .correct .answer.(B) .2.3333333
Consider .the .following .method.
public .ArrayList<Integer> .mystery(int .n)
{
.ArrayList<Integer> .seq .= .new .ArrayList<Integer>();
.for .(int .k .= .1; .k .<= .n; .k++)
.seq.add(new .Integer(k .* .k .+ .3));
.
. return .seq
With Correct Solutions 2024
Consider .the .following .method.
public .static .int .mystery(int[] .arr)
{
.int .x .= .0
for .(int .k .= .0; .k .< .arr.length; .k .= .k .+ .2)
.x .= .x .+ .arr[k]
return .x;
}
Assume .that .the .array .nums .has .been .declared .and .initialized .as .follows.
.int[] .nums .= .{3, .6, .1, .0, .1, .4, .2};
(A) .5
(B) .6
(C) .7
(D) .10
(E) .17 .- .correct .answer.(C) .7
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?
(A) .int .x .= .obj.getA();
(B) .int .x;
.obj.getA(x);
(C) .int .x .= .obj.myA
(D) .int .x .= .SomeClass.getA();
(E) .int .x .= .getA(obj); .- .correct .answer.(A) .int .x .= .obj.getA
Which .of .the .following .changes .to .SomeClass .will .allow .other .classes .to .access .but .not
.modify .the .value .of .myC .?
(A) .Make .myC .public.
(B) .public .int .getC()
.{ .return .myC; .}
(C) .private .int .getC()
.{ .return .myC; .}
(D) .public .void .getC(int .x)
(E) .private .void .getC(int .x)
.{ .x .= .myC; .} .- .correct .answer.(B) .public .int .getC()
.{ .return .myC; .}
Consider .the .following .code .segment.
.int .x .= .7;
.int .y .= .3;
.
.if .((x .< .10) .&& .(y .< .0))
.System.out.printIn("Value .is: ." .+ .x .* .y);
.else
.System.out.printIn("Value .is: ." .+ .x ./ .y);
What .is .printed .as .a .result .of .executing .the .code .segment
(A) .Value .is: .21
(B) .Value .is: .2.3333333
(C) .Value .is: .2
(D) .Value .is: .0
(E) .Value .is: .1 .- .correct .answer.(B) .2.3333333
Consider .the .following .method.
public .ArrayList<Integer> .mystery(int .n)
{
.ArrayList<Integer> .seq .= .new .ArrayList<Integer>();
.for .(int .k .= .1; .k .<= .n; .k++)
.seq.add(new .Integer(k .* .k .+ .3));
.
. return .seq