Sei sulla pagina 1di 16

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: Mark for Review
public class Employee { (1) Points
public String name = " Duke";
public int empId = 12105;
public float salary;

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 Mark for Review
method in the Shirt class?
(1) Points

getShirtSize() (*)
price
size
color

Correct

3. You design a Circle class with various fields and methods. Which of the following could Mark for Review
be fields in this class? Distinguish which of these are between the properties and
behavior. (1) Points

(Choose all correct answers)

radius (*)
calculateDiameter()
color (*)
calculateArea()
calculateCircumference()
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 Mark for Review
again removes the breakpoint.
(1) Points

True (*)
False

Correct

7. Which of the following are adequate definitions for components of the Spiral Model of Mark for Review
Development?
(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 Mark for Review
data type.
(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? Mark for Review


int x = 25 - 5 * 4 / 2 - 10 + 4; (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[]) { Mark for Review
int x = 100;
int y = x; (1) Points
y++;
System.out.println("Value of x is " + x);
System.out.println("Value of y is " + y);
}

Value of x is 0
Value of y is 1
Value of x is 100
Value of y is 1
Value of x is 100
Value of y is 1
Value of x is 100
Value of y is 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
// Fragment 1 (1) Points
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? Mark for Review


public class Hello { (1) Points
public static void main(String args[]) {
String str = ”Hello”;
str = ”World”;
System.out.println(str);
}
}

Hello
Hello World
World (*)
Hello
World

Correct

23. Java is a strongly typed language; therefore you must declare a data type for all Mark for Review
variables.
(1) Points

True (*)
False

Correct

24. Identify the variable declared in the given code. Mark for Review
public class Welcome { (1) Points
public static void main(String args[]) {
int a = 2;
System.out.println("a is" + a);
}
}

int
Welcome
a (*)
2

Correct
Section 4
(Answer all questions in this section)

25. The classes of the Java class library 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. Mark for Review
import package.className; (1) Points

One is the package name and the other is the classname.

True (*)
False
Correct

28. The JFrame and JOptionPane classes are in the javax.swing package. Which two will Mark for Review
import those classes?
(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[]) { (1) Points
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.
Section 4
(Answer all questions in this section)

31. The String class must be imported using java.lang.String; Mark for Review
(1) Points

True
False (*)

Correct

32. What is the output? Mark for Review


public static void main(String args[]) { (1) Points
String greeting = "Java World!";
String w = greeting.replace("a", "A");
System.out.println(w);
}

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 together
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 Mark for Review
information on the accountholder’s name, balance, and interest rate. The best
approach is store 30000 separate variables in the main method. (1) Points

True
False (*)

Correct
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? Mark for Review
Employee emp = new Employee(); (1) Points
emp.calculateSalary(100000, 3.2, 15);

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 Mark for Review
fragment will produce the desired result.
(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 Mark for Review
statement should you use?
(1) Points
nextInt(81); (*)
nextInt(80);
nextInt();
nextInt(0-79);

Correct

39. Which class is used to generate 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 Mark for Review
content of the String.
(1) Points

True (*)
False

Correct

43. What is the output? Mark for Review


public static void main(String[] args) { (1) Points
String name = "Java";
String language = "Programming";
String fullName = name + language;
boolean test = fullName.equals(name + language);
System.out.println(test);
}

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. Mark for Review
Under what conditions does “Eligible for a bonus” print?
(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 Mark for Review
does “You qualify for a discount” print? (Hint: There may be more than one correct
answer) (1) Points

int purchase;
int rewardPoints;
if (purchase >= 2000 || rewardPoints >= 4000) {
System.out.println("You qualify for discount");
}

(Choose all correct answers)


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? Mark for Review


char grade = 'A'; (1) Points
switch (grade) {
case 'A':
System.out.println("Congratulations!"); case 'B':
System.out.println("Good work");
case 'C':
System.out.println("Average");
case 'D':
System.out.println("Barely passing");
case 'F':
System.out.println("Failed");
}
Failed
Congratulations!
Congratulations! Good Work Average Barely Passing Failed (*)
A

Correct

Previous Page 10 of 10 Summary

Potrebbero piacerti anche