Card 0 of 462
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 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).
Compare your answer with the correct one above
Consider the method
public String mystery(String s)
{
String s1 = s.substring(0,1);
String s2 = s.substring(1,2);
String s3 = s.substring(2, s.length() - 2);
String s4 = s.substring(s.length() - 2, s.length() - 1);
String s5 = s.substring(s.length() - 1);
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"));
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.
String s1 = s.substring(0,1);
String s2 = s.substring(1,2);
String s3 = s.substring(2, s.length() - 2);
String s4 = s.substring(s.length() - 2, s.length() - 1);
String s5 = s.substring(s.length() - 1);
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:
s1 = "A"
s2 = "B"
s3 = "NORMALITI"
s4 = "E"
s5 = "S"
Because s is longer than 5 characters, we take the else
, so it returns the following:
A + B + mystery(NORMALITI) + E + S
Next, we repeat with the new argument. s = NORMALITI, so after the first part, the result is:
s1 = "N"
s2 = "O"
s3 = "RMALI"
s4 = "T"
s5 = "I"
Because s is longer than 5 characters again, we take the else
, so it returns the following:
N + O + mystery(RMALI) + T + I
Which gets added to the previous return, making it this:
A + B + N + O + mystery(RMALI) + T + I + E + S
Once again, we repeat with the argument s = RMALI. After the first part, the result is:
s1 = "R"
s2 = "M"
s3 = "A"
s4 = "L"
s5 = "I"
Because s is less than or equal to 5 characters in length, we take the if
this time. It returns the following:
I + L + A + M + R
We can replace all instances of mystery(RMALI) with the above, so the original return becomes this:
A + B + N + O + I + L + A + M + R + T + I + E + S
Which gets printed as ABNOILAMRTIES, the answer.
Compare your answer with the correct one above
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;
}
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;
When i = 0,
arr[0] == 1
arr[1] == 2
temp = 1
arr[0] = 2
arr[1] = 1
arr[] == {2, 1, 3, 4, 5}
When i = 1
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}
When i = 3
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.
Compare your answer with the correct one above
The function fun
is defined as follows:
public int fun(int[] a)
{
a[a.length - 1] = a[0];
return a[0] + (a[0] % 2);
}
What is the value of a[0]
after the following code segment is executed?
int[] a = {3, 6, 9, 12};
a[0] = fun(a);
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
.
Compare your answer with the correct one above
What are the values of 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;
}
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.
Compare your answer with the correct one above
What is the error in the following code?
int val1 = -14,val2 = 4;
final int val3 = 9;
double val4 = 4.1;
double val5 = 3.1;
val1 = val2 * val3;
val3 = val1 * 12;
val5 = val1 - val3;
val4 = val2 + val5;
The only error among the options given is the fact that this code assigns a new value to the variable val3
, which is defined as a constant. (This is indicated by the keyword final
before the rest of its declaration.) You cannot alter constants once they have been declared. Thus, the following line will cause a compile-time error:
val3 = val1 * 12;
Compare your answer with the correct one above
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?
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]);
Compare your answer with the correct one above
public static void main(String[] args) {
int[] x = {3,4,4,5,17,4,3,1};
int[] y = remove(x,4);
for(int i = 0; i < y.length; i++) {
System.out.print(y[i] + " ");
}
}
``
public static boolean remove(int[] arr, int val) {
boolean found = false;
int i;
for(i = 0; i < arr.length && !found; i++) {
if(arr[i] == val) {
found = true;
}
}
if(found) {
for(int j = i; j < arr.length;j++) {
arr[j - 1] = arr[j];
}
arr[arr.length - 1] = 0;
}
return found;
}
What is the error in the code above?
The problematic line is this one:
int[] y = remove(x,4);
Notice that the variable y is defined as an array. Now, it is tempting to think (without looking too closely) that the remove
method returns the array after the removal has been accomplished; however, this is not how the logic works in the remove
method. Instead, it returns a boolean indicating whether or not this removal was successful or not (i.e. it tells you whether or not it actually found the value). Therefore, you cannot make an assignment like the one above, for the two types are not the same. That is, y
is an integer array, while remove
returns a boolean value!
Compare your answer with the correct one above
public interface ServerInstance {
byte[] readBytes();
boolean writeBytes(byte[]b);
boolean wake();
boolean status();
void sleep();
}
``
public class MyHost implements ServerInstance {
boolean running = false;
public boolean wake() {
// Other logic code here...
return running;
}
public boolean status() {
// Other logic code here...
return running;
}
public byte[] readBytes() {
byte[] buffer = null;
// Other logic code here...
return buffer;
}
public void sleep() {
// Other logic code here...
running = false;
}
public byte[] writeBytes(byte[] b) {
// Other logic code here...
return b;
}
// Other methods...
}
What is the error in the code above?
When you implement an interface, all of the methods defined in that interface must be written in the class that is proposing to be such an implementation. (Or, if they are not implemented there, you need to involve abstract classes—but that is not our concern here.) The methods must match the prototypes proposed in the interface. In the example code, ServerInstance
has a method writeBytes
that returns a boolean value. However, the MyHost
class has implemented this method as returning a byte[]
value. Since you cannot have different types of return values for methods with the same parameter set, Java interprets this as being the proposed implementation for writeBytes(byte[] b)
, and this method must return a boolean if MyHost
is to implement ServerInstance
.
Compare your answer with the correct one above
public static void foo() {
int x = 10; y = 21, z = 30;
int[] arr = null;
for(int i = 0; i < y; i+= 4) {
arr = new int[i / 5];
}
for(int i = 0; i < x; i++) {
arr[i] = z / i;
}
for(int i = 0; i < z; i++) {
arr[i] = z * i;
}
}
Which of the following lines of code will cause a compile-time
error?
An error in compilation occurs before any code even attempts to execute. Thus, it is primarily a syntactical error in the code. In the selection above, the line
int x = 10; y = 21, z = 30;
has a semicolon right after 10_._
This causes an error in the code directly following on this, for the code
y = 21, z = 30;
is not valid Java code.
There are other errors in this code. arr[i] = z / i;
will cause a divide by 0 error when iis 0. Also, arr[i] = z * i;
will overrun the bounds of the array arr
. However, these are not compile-time errors—i.e. errors that occur before the code is even able to run!
Compare your answer with the correct one above
class Base{};
class Derived : public Base{
public:
void method(){ cout<< "method1
"; }
};
class Derived2 : public Base{
public:
void method() { cout<< "method2
"; }
};
int main(){
Base* bp = new Derived();
Derived2* d2p = bp;
d2p -> method();
}
What is the result of compiling and running the program in C++?
In this problem, Derived1 and Derived2 are children of the Base class. If we take a look at this line:
Base* bp = new Derived();
We are assigning a new Derived class to a base pointer. This will compile. Think of the Base as a larger object because it is the parent, so copying a smaller object into a larger one is acceptable.
Now let's look at this one:
Derived2* d2p = bp;
This line will cause the program to not compile. Since the Base class is considered the "bigger" object, copying a bigger object into a "smaller" one will result in a failure to copy everything over, this is known as a Slicing Problem.
We don't even have to look at the next line because we know that the program wil crash.
Compare your answer with the correct one above
Given:
const int x = 10;
Which of the following will compile?
First we take a look at the given statement:
const int x = 10;
the const in front of "int" means that x will always hold the value of 10 and it will not change.
Let's observe all the choices.
int *p =&x
This line says to assign the address of x (in memory) to the pointer p. This however, will not compile because int * p is not marked as const. x is marked as a const so this forces int * p to be a const as well.
int * const q = &x
There is a const in this case but it is in the wrong place
const int * r = &x
The const is in the correct place and this is the correct answer
Compare your answer with the correct one above
class Base{
protected:
void method();
};
class Derived : public Base{
};
int main(){
Base b;
b.method(); //Line A
Derived d;
d.method(); //Line B
}
Which of the following is true?
To understand this question, we have to understand what protected method means. A protected method is a method that is accessible to methods inside it's own class as well as it's children. This means that a protected method can be called in the child class.
We can see that method() is called inside main. This should already raise a red flag. A protected class is being called outside of the child class so it will not compile. Even those it's being called on the Base and Derived objects, the calls are not made inside Base and Derived class so neither line will compile.
Compare your answer with the correct one above
Consider the following code:
int num = 1;
for(int i = 0; i < 10; i++) {
num *= i;
}
The code above is intended to provide the continuous product from num to 10. (That is, it provides the factorial value of 10!.) What is the error in the code?
This loop will indeed run 10 times. However, notice that it begins on 0 and ends on 10. Thus, it will begin by executing:
num *= 0
Which is the same as:
num = num * 0 = 0
Thus, you need to start on 1 for i. However, notice also that you need to go from 1 to 10 inclusive. Therefore, you need to change i < 10 to i <= 10.
Compare your answer with the correct one above
Consider the code below:
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 issues could be raised regarding the code?
Recall that a "mutator" is a method that allows you to change the value of fields in a class. Now, for the setTime
, setMinutes
, and setSeconds
methods, you do not check several cases of errors. For instance, you could give negative values to all of these methods without causing any errors to be noted. Likewise, you could assign a minute or second value that is greater than 60, which could cause errors as well. Finally, you could even make the clock have a value that is greater than "23:59:59", which does not make much sense at all!
Compare your answer with the correct one above
What is wrong with the following code?
If you notice, in our for loop, the integer j is used as the iteration variable. However, no where in the code is that variable defined. To fix this issue this could have been done.
for (int j=0;j<3,j++)
Note the bold here is inserted. We need to define j here. We could have also defined j as a double.
Compare your answer with the correct one above
Consider the following C++ code:
1. #include
2. #include
3. using namespace std;
4. int main() {
5. ifstream outputFile;
6. inputFile.open("TestFile.txt");
7. outputFile << "I am writing to a file right now." << endl;
8. outputFile.close();
9. //outputFile << "I'm writting on the file again" << endl;
10. return 0;
11. }
What is wrong with the code?
Type ifstream objects are used to read from files, NOT to write to a file. To write to a file you can use ofstream. You can also you fstream to both read and write to and from a file.
Compare your answer with the correct one above
a. public void draw() {
b. int i = 0;
c. while(i < 15){
d. system.out.println(i);
e. i++
f. }
g. }
Which lines of code have errors?
line d's error is that system.out.println (i); is not capitalized.
line e's error is that there is a missing semicolon.
Compare your answer with the correct one above
Consider the following code:
public static int\[\] del(int\[\] a,int delIndex) {
if(delIndex < 0 || delIndex >= a.length) {
return null;
}
int\[\] ret = new int\[a.length - 1\];
for(int i = 0; i < a.length; i++) {
if(i != delIndex) {
ret\[i\] = a\[i\];
}
}
return ret;
}
What is the error in the code above?
The problematic line in the code above is the one that reads:
ret\[i\] = a\[i\];
This is going to work well until the end of the array. The variable i is going to go for the length of the array a. However, the array ret is one less in length. This means that you will overrun your array, getting an ArrayIndexOutOfBoundsException at the very end of the looping. You would need to implement two indices—one of which will not be incremented on that one time when you skip the element that is removed from the array.
Compare your answer with the correct one above
What is wrong with this loop?
int i = 0;
int count = 20;
int\[\] arr = \[1, 3, 4, 6, 8, 768, 78, 9\]
while (i < count) {
if (i > 0) {
arr\[i\] = count;
}
if (i < 15) {
System.out.println("HEY");
}
}
The loop is infinite because i never gets incremented to become greater than count.
The loop in this case is,
while (i < count) {
if (i > 0) {
arr\[i\] = count;
}
and the error occurs because there is no terminating factor in the while loop. A terminating factor would be,
while(i<count){
if(i>0, i<10, i++){
arr\[i\] = count;
}
Compare your answer with the correct one above