1)a)A Java compiler is a program that translates code written in the language Java to byte code. The language Java is human readable, but the byte code is not.
The byte code can then be run through an interpreter which actually executes the byte code by converting it to machine instructions.
b)I have a comment about this question: it's ambiguous! However, if I had to define a comment, I'd say: "A comment is a message that is ignored by the compiler or interpreter. The purpose of a comment is to include a (human readable) note or message that is designed to make the code easier to understand. This is useful if you don't look at your code for a long time or have to give your code to someone else. In Java, there are 2 kinds of comments: 1) Single lined comments starting with // and multi line comments which go between /* and */"
c)Loop : (same ambiguous comment as before) A loop is a programming construct that allows you to execute a statement or group of statements several times. It generally will have a condition which determines whether the statement or group of statements will be executed. After the statement or group of statements is executed, the condition will be rechecked and the block of code potentially repeated.
d)A method is a named programming construct that allows you to give it input and output.
e)null : null is an "empty" value in Java. It only refers to reference types and is a placeholder for when a reference variable does not contain an actual Object.
2)
true
true
true
true
true
true
true
true
3)
a)
Ha
99
b)(Note this question uses references, so it wouldn't be asked on the fall 2011 midterm)
a1 and a2 are aliases of each other. This means if a1[i] is changed then a2[i] will also be changed (and vice versa)
After the lines setting values execute we have:
Start: {5.0,2.0,3.5, 4.0,1.0};
a1[0] = a1[0] + a1[1] = 5.0 + 2.0 ----> {7.0,2.0,3.5,4.0,1.0}
a2[1] = i1 / i2 = 3 / 4 = 0 ----> {7.0, 0.0, 3.5, 4.0, 1.0}
a2[2] = (int)a2[2] * 3 = 3 * 3 = 9 ---> {7.0, 0.0, 9. 4.0, 1.0}
a2[3] *= a2[3] + 1.0 is the same as a2[3] = a2[3] * (a2[3] + 1.0) = 4 * 5 = 20 ---> {7.0, 0.0, 9.0, 20.0, 1.0}
a2[4] = 3.0 * (3 % 4) ---> 9 ---> {7.0, 0.0, 9.0, 20.0, 9.0}
So it will print:
7.0
0.0
9.0
20.0
9.0
c)
This question is a bit EVIL! It is a trick question for so many reasons....
For starters, the order of operations says to do + before ==. That is the main trick of this question. So this means it is testing, whether
"Animals are equals: " + "catch-22" is equal to "catch-" + 22
Since these are not equal it prints "false"
[Side rant: An interesting, and VERY evil question would be if the parenthesis were around (pig == dog) In this case, you'd be misled.
Remember that == should not be used on Strings. This is since String is a reference type. When you use == it will compare the ADDRESS stored in the variable.
It will not store the contents stored in the String itself. This would lead you to believe that pig == dog would be false since it's 2 different Strings.
However, in the case Java compiler will make an optimization and use the same String for both variables. This is since it knows ahead of time that only a
String literal will be used. You are not responsible for knowing this, but you ARE responsible for knowing that you should use .equals on Strings:
e.g. pig.equals(dog) should be used instead of pig == dog
]
4)a)
(m == 3 && d == 26) || (m == 3 && d == 29)
Note:
It would not be technically correct to put
if ((m==3 && d == 26) || (m==3 && d == 29))
The question asks to output a boolean expression, not an if statement.
b)
(c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z')
c)
There are a few ways to do this:
(a % 2 == 1 && b % 2 == 1 && c % 2 == 0) || (a % 2 == 1 && b % 2 == 0 && c % 2 == 1) || (a % 2 == 0 && b % 2 == 1 && c % 2 == 1)
is the most straightforward way.
There is a neat trick you could do though:
(a % 2) + (b % 2) + (c % 2) == 2
5)This question is really long.....I won't ask you anything like this as it's too time consuming to write out. However, you should understand the answers.
Also, I think there is a mistake in the question. There is no method "trace" It is a method mystify()
12 array should be == true
4)Line 14 : Run time error : ArrayOutOfBounds because if i == 0 as on the first step, we have myNumbers[myNumbers.length] This should be myNumbers[myNumbers.length - i - 1]
5)I could not find another "error" However, I'm not sure what the purpose of the variable palindrome is on line 26. It does not appear to be used anywhere and perhaps is what's meant by "semantic" error.
7)
public class ArrayUtils {
public static String[] insert(String[] sorted, String newString) {
//first take care of annoying case where there are no elements in the array.
if (sorted.length == 0) {
String[] newArray = new String[1];
newArray[0] = newString;
return newArray;
}
//now we can assume the array sorted has at least one element.
//We want to find the first element in it that is AFTER newString in lexicographic order
//we will use the compareTo method of String:
int firstPast = 0;
while (firstPast < sorted.length && newString.compareTo(sorted[firstPast]) > 0) {
firstPast++;
}
//now we can insert the String newString at location firstPast
//first we'll make a new array and copy values UPTO firstPast into the new array:
String[] newSortedArray = new String[sorted.length + 1];
for (int i = 0; i < firstPast; i++) {
newSortedArray[i] = sorted[i];
}
//now add newString to the array:
newSortedArray[firstPast] = newString;
//now copy the 2nd part of the array over:
for (int i = firstPast; i < sorted.length; i++) {
newSortedArray[i+1] = sorted[i];
}
return newSortedArray;
}
}
Part2:
import java.util.Scanner;
public class DictionaryCompiler {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter a line (empty line to stop):");
String newString = reader.nextLine();
String[] words = new String[0];
//as long as the string is not equal to empty string
while (!newString.equals("")) {
//put the statements in this order so that we don't add empty string to the words array.
words = ArrayUtils.insert(words, newString);
System.out.println("Enter a line (empty line to stop):");
String newString = reader.nextLine();
}
System.out.println("The lines you entered:");
for (int i = 0; i < words.length; i++) {
System.out.println("* + " + words[i]);
}
}
}