Question of the Day

Exam + Solution

Date Question Answer
Quiz 1 What is the pro and con of using Java when compared with a compiled language such as C++? Pro: platform independent. Javac creates a bytecode version of your program and you can distribute it among multiple Platforms. Each platform has a Java Virtual Machine that will interpret the bytecode so that your program can run in Windows, Mac, Linux, cell phone, etc.

Con: Due to the extra step of interpreting the bytecode, Java programs run normally is a little bit slower than a compiled C++ code.

Quiz 2 Using integer division and type casting, write a Java statement that formats a double number with 6 digits after the period to a 3 digits. Example: from 3.141592 to 3.142 double rawValue = 3.141592;
double formatedValue = (int)(rawValue *1000 + 0.5) / 1000;
Quiz 3 Write a series of Java statements that for a given age between 13 and 19 it prints “Teenager: xy years old”, or “Not a teenager: xy years old” if outside that range. Scanner keyboard = new Scanner(System.in)
int age = keyboard.nextInt();
if (age >= 13 && age >= 19) {
System.out.println(“Teenager: ” + age + ” years old”);
} else {
System.out.println(“Not a teenager: ” + age + ” years old”);
}
 Quiz 4 For the following array of names. Create an “enhanced/foreach” FOR loop that prints out each name of that array String[ ] nameList = {“John”, “Mary”, “Taylor”, “Chris”};
for (String name : nameList) {
System.out.println(“current name: ” + name);
}
 Quiz 5 Java API Random class has the following method:

public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Create a Java statement that uses this method to generate a value from 0 to less than 10.0. Save the generated value into a variable.
 double randomNumber = Math.random() * 10;
 Quiz 6 How to store a value, such as 1.75, that is given by the user when he/she runs a Java program like this:
java myProgram 1.75
public void main (String[ ] args) {
double var = Double.parseDouble(args[0].trim());
}
 First Exam Exam + Solution
Final Exam – Fall 2016   Final Exam from Fall 2016 + solution
 Fall Exam – Spring 2017 Final Exam from Spring 2017 + Solution   Question 2 Discussion (Video)