All Computer Science Resources
Example Questions
Example Question #1 : Information Hiding
Class Person {
int height;
float weight;
public:
int getHeight();
void setHeight(int);
float getWeight();
void setWeight(float);
};
What is the access level of height
and weight
?
virtual
public
private
protected
friend
private
Until an access specifier is given, all class members are implicitly private. All members defined after an access specifier is used will will have that access level until another access specifier is explicitly invoked. Since no access specifier was used, weight
and height
are automatically private.
Note that virtual
is not an access keyword.
Example Question #1 : Information Hiding
Consider the following code:
public static class Clock {
private int seconds;
public Clock(int s) {
seconds = s;
}
public void setTime(int s) {
seconds = s;
}
public void setSeconds(int s) {
int hoursMinutes = seconds - seconds % 60;
seconds = hoursMinutes + s;
}
public void setMinutes(int min) {
int hours = seconds / 3600;
int currentSeconds = seconds % 60;
seconds = hours + min * 60 + currentSeconds;
}
}
Which of the following represents a method that returns the minute value of the clock?
public int getMinutes() {
return seconds / 60;
}
public int getMinutes() {
int hours = seconds / 3600;
return (seconds - hours * 3600) / 60;
}
public int getMinutes() {
int hours = seconds / 3600;
return (seconds - hours * 3600);
}
public int getMinutes() {
return mins;
}
public int getMinutes() {
return seconds * 60;
}
public int getMinutes() {
int hours = seconds / 3600;
return (seconds - hours * 3600) / 60;
}
This clock class is defined as having only seconds in its fields, so you have to convert this value for any of the accessors and mutators. Therefore, you need to perform careful (though simple) mathematics for this conversion. To calculate the number of minutes in a given number of seconds, you first need to remove the hours from total count. First, compute the hours using integer division (which will drop the fractional portion of the decimal). This is:
int hours = seconds / 3600;
Next, you need to figure out how many seconds are in that value of hours. This is helpful precisely because integer division drops the decimal portion. Thus, you will subtract off:
hours * 3600
from the total seconds.
This gives you the seconds that apply to the minutes and seconds of the time. Therefore, you will finally need to divide by 60 to isolate the minutes.
Example Question #232 : Computer Science
Consider the following code:
public class Clock {
private int seconds;
public Clock(int s) {
seconds = s;
}
public void setTime(int s) {
seconds = s;
}
public void setSeconds(int s) {
int hoursMinutes = seconds - seconds % 60;
seconds = hoursMinutes + s;
}
public void setMinutes(int min) {
int hours = seconds / 3600;
int currentSeconds = seconds % 60;
seconds = hours + min * 60 + currentSeconds;
}
}
Which of the following defines a toString method that will output the time in 24-hour format in the following form:
1:51:03
(Notice that you need to pad the minutes and seconds. You can call 12 midnight "0".)
public String toString() {
int hours = seconds / 3600;
int mins = (seconds - hours * 3600);
String minString = ""+mins;
if(mins < 10) {
minString = "0" + minString;
}
int secs = seconds % 60;
String secString = "" + secs;
if(secs < 10) {
secString = "0" + secString;
}
return hours + ":"+minString+":"+secString;
}
public String toString() {
int hours = seconds / 3600;
int mins = (seconds - hours * 3600) / 60;
String minString = ""+mins;
if(mins < 10) {
minString += "0";
}
int secs = seconds % 60;
String secString = "" + secs;
if(secs < 10) {
secString += "0";
}
return hours + ":"+minString+":"+secString;
}
public String toString() {
int hours = seconds / 3600;
int mins = seconds - (hours * 3600) / 60;
String minString = ""+mins;
if(mins < 10) {
minString = "0" + minString;
}
int secs = seconds * 60;
String secString = "" + secs;
if(secs < 10) {
secString = "0" + secString;
}
return hours + ":"+minString+":"+secString;
}
public String toString() {
int hours = seconds / 3600;
int mins = (seconds - hours * 3600) / 60;
String minString = ""+mins;
if(mins < 10) {
minString = "0" + minString;
}
int secs = seconds % 60;
String secString = "" + secs;
if(secs < 10) {
secString = "0" + secString;
}
return hours + ":"+minString+":"+secString;
}
public String toString() {
int hours = seconds / 60;
int mins = seconds - (hours * 3600) / 60;
String minString = ""+mins;
if(mins < 10) {
minString = "0" + minString;
}
int secs = seconds * 60;
String secString = "" + secs;
if(secs < 10) {
secString = "0" + secString;
}
return hours + ":"+minString+":"+secString;
}
public String toString() {
int hours = seconds / 3600;
int mins = (seconds - hours * 3600) / 60;
String minString = ""+mins;
if(mins < 10) {
minString = "0" + minString;
}
int secs = seconds % 60;
String secString = "" + secs;
if(secs < 10) {
secString = "0" + secString;
}
return hours + ":"+minString+":"+secString;
}
There are several points to be noted in this code. First, you need to calculate each of the constituent parts from the seconds stored in the class. The "display value" of seconds is relatively easy. This is just the remainder of a division by 60. Consider:
61 seconds => This is really 1 minute and 1 second.
155 seconds => This is really 2 minutes and 35 seconds.
So, you know that the display seconds are:
seconds % 60
You must compute the hours using integer division (which will drop the fractional portion of the decimal). This is:
int hours = seconds / 3600;
Next, you need to figure out how many seconds are in that value of hours. This is helpful precisely because integer division drops the decimal portion. Thus, you will subtract off:
hours * 3600
from the total seconds.
This gives you the seconds that apply to the minutes and seconds of the time. Therefore, you will finally need to divide by 60 to isolate the minutes.
Then, you must pad the values. This is not done using +=
, which would add the "0" character to the end of the string. Instead, it is done by the form:
secString = "0" + secString;
Example Question #1 : Procedural Abstraction
Given a vector of ints called "intVec", write a "ranged for" loop, also sometimes known as a "for each" loop, to double the values fof all elements in a vector. (In C++)
for( int i = 0; i < intVec.size(); ++i){
intVec[i] *= 2;
}
for( int i : intVec(){
i * = 2;
}
for( int& i : intVec(){
i * 2;
}
for( int& i : intVec(){
i * = 2;
}
for( int i = 0; i < intVec.size(); ++i){
intVec[i] * 2;
}
for( int& i : intVec(){
i * = 2;
}
A "for each" loop was introduced in C++ 11 which allows the user to loops through a container of items without writing a traditional loop. Let's take a look at all the choices.
for( int i : intVec(){
i * = 2;
}
Although this looks like the correct answer, it's not. The int i in the first part of the for loop is passed in "by value", which means that a copy of intVec will be made and the integers inside the copy will be doubled. The integers inside the original intVec vector will be unaffected. To fix this problem, int i needs to be passed into the for loop "by reference", which is done by adding a "&" symbol after "int".
The correct loop is:
for( int& i : intVec(){
i * = 2;
}
Let's take a look at the other choices:
for( int& i : intVec(){
i = 2;
}
This one will not work because it is setting all items in intVec to 2, not doubling them.
All the other for loops are not "for each" loops so they are incorrect even if they accomplish the same output.
Example Question #1 : Program Analysis
The function fun
is defined as follows:
public int fun(int[] a)
{
a[a.length - 1] = a[0];
return a[0] + (a[0] % 2);
}
a[0]
after the following code segment is executed?int[] a = {3, 6, 9, 12};
a[0] = fun(a);
13
12
9
6
3
12
The first part of fun
assigns the value of the last location of the array to the first location. Then, it returns the a[0] + (a[0] % 2)
. That last portion will be a "1
" if a[0]
is odd, and "0
" if a[0]
is even. Since a[0] == 12
, which is even, the expression evaluates to 12 + 0
, which is 12
.
Example Question #231 : Computer Science
x
, y
, and z
after the following code is executed?int x = 4, y = 3, z;
for (int i = 0; i < 5; i++)
{
z = x + y;
y = x - y;
x = z;
}
x == 28, y == 4, z== 4
x == 28, y == 4, z== 28
x == 14, y == 2, z == 14
x == 16, y == 12, z== 16
x == 32, y == 24, z == 32
x == 28, y == 4, z== 28
The loop will run 5 times. The values of x
, y
, and z
after each run will be as follows:
i == 0: x == 7, y == 1, z == 7
i == 1: x == 8, y == 6, z == 8
i == 2: x == 14, y == 2, z == 14
i == 3: x == 16, y == 12, z == 16
i == 4: x == 28, y == 4, z == 28
At the end, i
will equal 5, and the values will no longer change.
Example Question #232 : Computer Science
if (s.length() <= 5)
return s5 + s4 + s3 + s2 + s1;
else
return s1 + s2 + mystery(s3) + s4 + s5;
}
What is the output of
System.out.println(mystery("ABNORMALITIES"));
ABNORMALITIES
SEITILAMRONBA
None of these answers is correct.
SEITRMALIONBA
ABNOILAMRTIES
ABNOILAMRTIES
The .substring()
method takes the character at the first number in the arguments, and goes through the String until it reaches the second number in the arguments, without copying the character at the second number.
In the first part of mystery()
, the Strings s1, s2, s3, s4, and s5 are made and filled. If n = # of characters in s, s1 gets the first character in s, s2 gets the second character in s, s3 gets the third through n-2 characters, s4 gets the n-1 character, and s5 gets the last character.
Let's look at the second portion of mystery().
if (s.length() <= 5)
return s5 + s4 + s3 + s2 + s1;
else
return s1 + s2 + mystery(s3) + s4 + s5;
The if
statement checks the length of s, and if it's less than or equal to 5, it returns a String made from s5, followed by s4, etc. If s were equal to "abcde", then the if
would evaluate to true, and would return "edcba". In recursion, this is known as the "base case".
The else
statement is for strings that are greater than 5 characters in length. It returns s1, followed by s2, then the result of mystery(s3), then s4 and s5. The fact that it calls itself makes this recursion.
Let's step through the example. The argument for mystery()
, s, is "ABNORMALITIES". After the first part, this is the result:
else
, so it returns the following:A + B + mystery(NORMALITI) + E + S
else
, so it returns the following:N + O + mystery(RMALI) + T + I
A + B + N + O + mystery(RMALI) + T + I + E + S
if
this time. It returns the following:I + L + A + M + R
A + B + N + O + I + L + A + M + R + T + I + E + S
Example Question #233 : Computer Science
Consider the Array
int[] arr = {1, 2, 3, 4, 5};
What are the values in arr
after the following code is executed?
for (int i = 0; i < arr.length - 2; i++)
{
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
23451
54321
15432
12345
OutOfBoundsException
23451
We start with an array, arr,
of size 5, containing {1, 2, 3, 4, 5}.
The loop in the code,
for (int i = 0; i < arr.length - 2; i++)
loops through the array up to the second to last cell, given that arr.length - 2
is the index of the second to last cell, and it starts at the first cell.
Let's look at the code inside the loop.
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
arr[0] == 1
arr[1] == 2
temp = 1
arr[0] = 2
arr[1] = 1
arr[] == {2, 1, 3, 4, 5}
arr[1] == 1
arr[2] == 3
temp = 1
arr[1] = 3
arr[2] = 1
arr[] == {2, 3, 1, 4, 5}
When i = 2
arr[2] == 1
arr[3] == 4
temp = 1
arr[2] = 4
arr[3] = 1
arr[] == {2, 3, 4, 1, 5}
arr[3] == 1
arr[4] == 3
temp = 1
arr[3] = 3
arr[4] = 1
arr[] == {2, 3, 4, 5, 1}
As the loop progresses, it moves whatever value is in arr[0]
all the way to the end of the array.
Example Question #231 : Computer Science
Identify a user error that could occur in this program
UserInput ui = new UserInput(); // input from the user
int s = (Integer)ui;
System.out.println(s);
The user input could not be an integer
There is nothing wrong
The parseInt statement is incorrect
The user can never do anything wrong
The user input could not be an integer
The user could input a string and the cast to an (Integer) would cause a runtime exception. If there is a runtime exception, the program will stop and open up vulnerabilities to hackers. Once a hacker knows how to halt a program, they can start input bad data to see a database schema to collect data. One way to fix this would be using a utility method such as parseInt(ui).
Example Question #1 : Debugging
Consider the following code:
public static class Rectangle {
private double width, height;
public Rectangle(double w,double h) {
width = w;
height = h;
}
public double getArea() {
return width * height;
}
public double getPerimeter() {
return 2 * width + 2 * height;
}
}
public static class Square extends Rectangle {
public Square(double s) {
super(s,s);
}
}
public static void main(String[] args) {
Rectangle[] rects = new Rectangle[6];
for(int i = 0; i < 6; i++) {
if(i % 2 == 0) {
rects[i] = new Rectangle(i+10,i + 20);
} else {
rects[i] = new Square(i+20);
}
}
Square s = rects[1];
}
What is the error in the code above?
You need to use "implements", not "extends" for the class Square
.
The final assignment operation cannot be done.
You cannot assign a Square
object to an array of Rectangle
objects.
There is an array overrun.
There is an error in the declaration of the Rectangle
array.
The final assignment operation cannot be done.
This code fills up the 6 member array with alternating Rectangle and Square objects. You can do this because the Square class is a subclass of Rectangle. That is, since Squares are Rectangles, you can store Square objects in Rectangle variables. However, even though rects[1]
is a square, you CANNOT immediately reassign that to a Square object. The code has now come to consider all of the objects in the array as being Rectangle objects. You would need to explicitly type cast this to get the line to work:
Square s = (Square)(rects[1]);
Certified Tutor
Certified Tutor