Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 2
(Answer all questions in this section) 1.
In the code example below, identify any methods: public class Employee { public String name = " Duke"; public int empId = 12105; public float salary;
Mark for Review (1) Points
public void displaySalary(){ System.out.println("Employee Salary: "+salary); } } name displaySalary() (*) salary empId Correct 2.
There are several fields and methods in a Shirt class. Which of the following could be a method in the Shirt class?
Mark for Review (1) Points
getShirtSize() (*) price size color Correct 3. You
design a Circle class with various fields and methods. Which of the following could be fields in this class? Distinguish which of these are between the properties and behavior. (Choose all correct answers) radius (*) calculateDiameter() color (*) calculateArea() calculateCircumference()
Mark for Review (1) Points
Correct 4.
Code within curly braces is called a “Block of code”.
Mark for Review (1) Points
True (*) False Correct 5. A
Java program can be written in the single line.
Mark for Review (1) Points
True (*) False Correct
Page 1 of 10
Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 2
(Answer all questions in this section) 6. A
breakpoint can be set by clicking a number in the left margin of the IDE. Clicking again removes the breakpoint.
Mark for Review (1) Points
True (*) False Correct 7.
Which of the following are adequate definitions for components of the Spiral Model of Development?
Mark for Review (1) Points
(Choose all correct answers) Test: Run the code and verify results (*)
Design: Plan the approach (*) Develop: Collect all specified instructions Requirements: Start the development Correct 8.
The Spiral Model reflects an iterative development process.
Mark for Review (1) Points
True (*) False Correct 9. A
software feature may allow the user to perform a specific task.
Mark for Review (1) Points
True (*) False Correct
Section 3
(Answer all questions in this section) 10.
The Java compiler automatically promotes byte, short, and chars data type values to int data type.
Mark for Review (1) Points
True (*) False Correct
Previous Page 2 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.
Section 3
(Answer all questions in this section) 11.
What is the correct way to cast a long to an int?
Mark for Review (1) Points
int longToInt = (int)20L; (*) int longToInt = 20L; int longToInt = 20L(int); int longToInt = int 20L; Correct 12.
What is parsing?
Mark for Review (1) Points
Converting numeric data to text Converting text to numeric data (*) Converting numeric data to a specified numeric data type Reading text from numeric data Correct 13.
What value is assigned to x? int x = 25 - 5 * 4 / 2 - 10 + 4;
Mark for Review (1) Points
8 9 (*) 34 7 Correct 14.
Which keyword makes a variable’s value unchangeable?
Mark for Review (1) Points
const final (*) static break Correct
15.
What is the output? public static void main(String args[]) { int x = 100; int y = x; y++; System.out.println("Value of x is " + x); System.out.println("Value of y is " + y); } Value Value Value Value Value Value Value Value
of of of of of of of of
x y x y x y x y
is is is is is is is is
Mark for Review (1) Points
0 1 100 1 100 1 100 101 (*)
Correct
Previous Page 3 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 3
(Answer all questions in this section) 16.
In Java, char is a primitive data type, while String is an object data type.
Mark for Review (1) Points
True (*) False Correct 17. An
Object cannot have String objects as properties.
Mark for Review (1) Points
True False (*) Correct
18.
The print() method prints to the console and automatically creates a line.
Mark for Review (1) Points
True False (*) Correct 19.
These two code fragments perform the same task.
Mark for Review (1) Points
// Fragment 1 String inputString = JOptionPane.showInputDialog("??"); int input = Integer.parseInt(inputString); input++; // Fragment 2 int input = Integer.parseInt(JOptionPane.showInputDialog("??")) + 1; True (*) False Correct 20.
Which two statements are true about the Scanner class?
Mark for Review (1) Points
(Choose all correct answers) A Scanner’s delimiter can be changed. (*) A Scanner object opens a stream for collecting input. (*) A Scanner object doesn’t have fields and methods. Scanners cannot read text files. Incorrect. Refer to Section 3 Lesson 5.
Previous Page 4 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 3
(Answer all questions in this section)
21.
The Scanner class considers space as the default delimiter while reading the input.
Mark for Review (1) Points
True (*) False Correct 22.
What is the output? public class Hello { public static void main(String args[]) { String str = ”Hello”; str = ”World”; System.out.println(str); } }
Mark for Review (1) Points
Hello Hello World World (*) Hello World Correct 23.
Java is a strongly typed language; therefore you must declare a data type for all variables.
Mark for Review (1) Points
True (*) False Correct 24.
Identify the variable declared in the given code. public class Welcome { public static void main(String args[]) { int a = 2; System.out.println("a is" + a); } } int Welcome a (*) 2 Correct
Mark for Review (1) Points
Section 4
(Answer all questions in this section) 25.
The classes of the Java class li brary are organized into packages.
Mark for Review (1) Points
True (*) False Correct
Previous Page 5 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 4
(Answer all questions in this section) 26.
Which two are valid import statements of the Scanner class?
Mark for Review (1) Points
(Choose all correct answers) import java.util; import java.*; import java.util.*; (*) import java.util.Scanner; (*) Correct 27.
The import statement consists of two parts. import package.className; One is the package name and the other is the classname. True (*) False
Mark for Review (1) Points
Correct 28.
The JFrame and JOptionPane classes are in th e javax.swing package. Which two will import those classes?
Mark for Review (1) Points
(Choose all correct answers) import javax.swing.*; (*) import javax.swing; import javax.swing.JOptionPane; import javax.swing.JFrame; (*) import javax.swing.J*; Correct 29.
The indexOf() method returns the index value of a character in the string.
Mark for Review (1) Points
True (*) False Correct 30.
What is the output?
Mark for Review
public static void main(String args[]) { String greeting = "Java World!"; String w = greeting.substring(7, 11); System.out.println(w); } rld! (*) ld! orld! rld Correct
Previous Page 6 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.
(1) Points
Section 4
(Answer all questions in this section) 31.
The String class must be imported u sing java.lang.String;
Mark for Review (1) Points
True False (*) Correct 32.
What is the output? public static void main(String args[]) { String greeting = "Java World!"; String w = greeting.replace("a", "A"); System.out.println(w); }
Mark for Review (1) Points
JavA World! JAvA World! (*) Java World! JAva World! Correct 33.
Which of the following scenarios would be ideal for writing a method?
Mark for Review (1) Points
When you don’t find similar lines of code to describe an object’s behavior. For every five to six lines of code. To group similar data types t ogether When you don’t want to repeat similar lines of code to describe an object’s behavior. (*) Correct 34. You’re
designing banking software and need to store 10000 customer accounts with information on the accountholder’s name, balance, and interest rate. The best approach is store 30000 separate variables in the main method. True False (*) Correct
Mark for Review (1) Points
35.
Methods allow all instance of a class to share same behaviors.
Mark for Review (1) Points
True (*) False Correct
Previous Page 7 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 4
(Answer all questions in this section) 36.
Which of the following are the arguments in the following method? Employee emp = new Employee(); emp.calculateSalary(100000, 3.2, 15);
Mark for Review (1) Points
calculateSalary(100000, 3.2, 15); 100000, 3.2, 15 (*) emp emp.calculateSalary(100000, 3.2, 15); Correct 37. You
need to generate random integer values in the range 2 through 10. This code fragment will produce the desired result.
Mark for Review (1) Points
Random r = new Random(); r.nextInt(9) + 2; True (*) False Correct 38. You
need to generate random integer values between 0 and 80 (inclusive). Which statement should you use?
Mark for Review (1) Points
nextInt(81); (*) nextInt(80); nextInt(); nextInt(0-79); Correct 39.
Which class is used to generat e random numbers?
Mark for Review (1) Points
Random (*) Number Integer Double Correct 40.
The Math class methods can be called without creating an instance of a Math object.
Mark for Review (1) Points
True (*) False Correct
Previous Page 8 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 4
(Answer all questions in this section) 41.
What is the package name which contains Math class?
Mark for Review (1) Points
java.awt java.net java.lang (*) java.io
Incorrect. Refer to Section 4 Lesson 5.
Section 5
(Answer all questions in this section) 42. A
String comparison with == compares the Strings’ locations in memory and not the content of the String.
Mark for Review (1) Points
True (*) False Correct 43.
What is the output? public static void main(String[] args) { String name = "Java"; String language = "Programming"; String fullName = name + language; boolean test = fullName.equals(name + language); System.out.println(test); }
Mark for Review (1) Points
False JavaProgramming Java Programming True (*) Correct 44.
Which three are conditional statements?
Mark for Review (1) Points
(Choose all correct answers) switch statement (*) for loop do while loop if/else statement (*) if statement (*) Correct
45.
How should Strings be compared?
Mark for Review (1) Points
The equals() method (*) ~= == = Correct
Previous Page 9 of 10 Next Summary
Test: Java Foundations Midterm Exam
Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer. Section 5
(Answer all questions in this section) 46. An
employee is eligible for a bonus based on certain criteria. Under what conditions does “Eligible for a bonus” print?
Mark for Review (1) Points
int rating; int experience; if (rating > 1 && experience == 5) { System.out.println (“Eligible for a bonus”); } 5 experience and 2 or more rating (*) 5 experience and 1 rating Less than 5 experience and 1 rating. 5 rating and 1 experience Correct 47. A
customer is eligible for a discount based on certain criteria. Under what conditions does “You qualify for a discount” print? (Hint: There may be more than one correct answer) int purchase; int rewardPoints; if (purchase >= 2000 || rewardPoints >= 4000) { System.out.println("You qualify for discount"); } (Choose all correct answers)
Mark for Review (1) Points
When rewardPoints is more than 1000 and purchase is 1000 When rewardPoints is more than 2000 or purchase greater than 1000 When purchase is 2000 regardless of the value of rewardPoints (*) When purchase is 4000 and rewardPoints is 2000 (*) Correct 48.
Which two are not logical operators?
Mark for Review (1) Points
(Choose all correct answers) ! || % (*) + (*) && Correct 49.
Which two statements are true about the default statement?
Mark for Review (1) Points
(Choose all correct answers) The default statement is optional in switch statement. (*) A default statement is required in every switch statement. When the input does not match any of the cases, the default statement is executed. (*) A default statement is executed by default when the program is executed. Correct 50.
What is the output? char grade = 'A'; switch (grade) { case 'A': System.out.println("Congratulations!"); System.out.println("Good work"); case 'C': System.out.println("Average"); case 'D': System.out.println("Barely passing"); case 'F': System.out.println("Failed"); }
Mark for Review (1) Points
case 'B':
Failed Congratulations! Congratulations! Good Work Average Barely Passing Failed ( *) A Correct
Previous Page 10 of 10 Summary