All Computer Science Resources
Example Questions
Example Question #1 : Console Output
public static void main(String[] args) {
int[][] x = {{4,5,6,7},{91,15,14,13}};
int[][] y = {{-13,4,41,14},{14,5,13,3}};
int[][] z = doWork(x,y);
for(int i = 0; i < z.length; i++) {
for(int j = 0; j < z[0].length;j++) {
System.out.print(z[i][j] + " ");
}
System.out.println();
}
}
public static int[][] doWork(int[][] a, int[][] b) {
int[][] ret = new int[a.length][a[0].length];
for(int i = 0; i < a.length; i++) {
for(int j = 0; j < a[i].length; j++) {
ret[i][j] = a[i][j] + b[i][j];
}
}
return ret;
}
What is the console output for the code above?
4 4 6 7
14 5 13 3
68
168
22 133
46 35
236
-9 9 47 21
105 20 27 16
-9 9 47 21
105 20 27 16
The doWork
method implements a relatively standard type of 2D array iteration, one that goes through each element. The outer loop goes through the first dimension of the array. Then, the inner loop provides index for the second dimension. From this, you get the total 2D index [i][j]
. The line ret[i][j] = a[i][j] + b[i][j];
actually performs the operation at the given index. Here, it performs an addition, combining the values at [i][j]
found in the two arrays. This gives you the sum at each index of ret
. The main method outputs each of these values, following the same standard algorithm for 2D array traversal.
Example Question #2 : Console Output
int a = 10, b = 5;
for(int i = 0; i < a; i+=2) {
for(int j = 0; j < i % b; j++) {
System.out.print("* ");
}
System.out.println();
}
What will be the console output for the code above?
* *
* * *
*
*
* *
* * *
* *
*
* *
* * *
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
* *
* * * *
*
* * *
* * *
* * *
* *
* *
* *
* * * *
*
* * *
All this problem requires is careful attention to the loop control variables. Notice that the first loop adds 2 to i every looping. This means that it will run for 0,2,4,6, and 8. Thus, a total of five lines will be printed.
Now, the second loop dictates the number of * characters that will be output per line. This is going to be based upon the result of the modulus i % b. Remember, modulus is a remainder calculation. Thus, you will have:
0 % 5 = 0
2 % 5 = 2
4 % 5 = 4
6 % 5 = 1
8 % 5 = 3
Hence, you will have the following (note the first line is empty, given the result of 0 % 5):
* *
* * * *
*
* * *
Example Question #1 : Console Output
What will the following code result in?
int main(){
int i = 7;
const int * ip = &i;
cout<< *ip << endl;
}
None of the above.
7
Runtime Error
8
Compilation error
7
Let's take a look at the code.
int i = 7;
This line assigns 7 to the variable "i".
const int * ip = &i;
This line creates a constant int pointer and assigns the address of "i" to the pointer.
cout << *ip << endl;
This line prints the dereference of the pointer, which holds the value 7.
Example Question #2 : Console Output
class Base{
public:
void foo(int n) { cout << "Base::foo(int)\n"; }
};
class Derive: public Base{
public:
void foo(double n) { cout << "Derived::foo(double)\n";}
};
int main(){
Derived der;
der.foo(42);
}
The above code is written in C++, what is the output of the program?
The program does not generate any output.
The program does not compile.
None of the above.
Base::foo(int).
Derived::foo(double).
Derived::foo(double).
This is an example of inheritance. The child class (Derived) is an inherited class of the parent class (Base). When the Derived object is created and the "foo" method is called, foo in the Derived class will be called. If there is the same method in the parent class and child class, the child's method will be called.
Example Question #1 : Console Output
LOGIC WITH 2-D ARRAYS
Given:
int[][] myArray = { {1, 2},
{3, 4} };
What would the following statement print out to the console?
System.out.print(myArray[1][1] + 10);
13
14
11
12
14
Before anything is printed out into the console, the following is first evaluated:
myArray[1][1] + 10
myArray[1][1] is referring to the item that is in row=1 and column=1 of myArray. Taking note that arrays start with row 0 and column 0, we see that the item in row 1 column 1 is the number 4. Now we have the following: 4+10. This evaluates to 14. Therefore, the Java print statement will print out the number 14 on the console.
Example Question #41 : Program Implementation
What is the output of this program?
arr = ["hello", "my", "name", "is"]
for (int i = 0; i < arr.length - 1; i++) {
System.out.println(arr[i]);
}
System.out.println("Sandra");
hello my name is
Sandra
hello my name is Sandra
hello
my
name
is
Sandra
hello
my
name
is
Sandra
The words are printed on separate lines due to the System.out.println() call and then whatever is inside of the parentheses is printed on the next line. This call is different from System.out.print() which prints words on the same lines.
Therefore, the Sysytem.out.printIn gives("Sandra"):
hello
my
name
is
Sandra
Example Question #2 : Console Output
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
False
True
False
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Example Question #3 : Console Output
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
This function doesn't do anything.
This method returns true if the number is evenly divisible by the divisor. It returns false otherwise.
This function returns false if the number is evenly divisible by the divisor. It returns true otherwise.
This function returns true or false.
This method returns true if the number is evenly divisible by the divisor. It returns false otherwise.
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Example Question #4 : Console Output
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String [] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
Yes it compiles and it prints bad job.
Yes it compiles and it prints good job.
No, it doesn't compile.
No, it doesn't compile.
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Example Question #1 : Iterations
int var=0;
int array[4];
for (int j=0; j<=4;j++)
{
var=var+1;
}
What is the value of var?
4
10
8
5
6
5
The for loop simply sums up the numbers 0 through 3, which is 6.
First iteration through the loop, j=0:
var=0+1=1
Second iteration, j=1:
var=1+1=2
Third iteration, j=2:
var=2+1=3
Fourth iteration, j=3:
var=3+1=4
Fifth iteration, j=4:
var=4+1=5