Documente Academic
Documente Profesional
Documente Cultură
3. A breakpoint can be set by clicking a number in the left margin of the IDE. Clicking again
removes the breakpoint.
True (*)
False
Correct
7. You have a beautiful garden at home. On Sunday, you start budding your rose plant to
make few more samples of rose plants to plant in the garden. Can you categorize how this
scenario could be represented by classes and instances?
Rose plant is the class and the samples generated from the rose plant are instances of that
class. (*)
Samples are the class and the rose plant is the instances of samples.
Samples of the rose plant are called classes and not the actual rose plant.
Rose plant is the object and samples are not instances of the plant because they have not
grown yet.
Correct
8. There are several fields and methods in a Shirt class. Which of the following could be a
method in the Shirt class?
color
price
size
getShirtSize() (*)
Correct
9. In object oriented programming, there is an emphasis on which of the following two:
Writing algorithms.
Modeling objects.(*)
Creation of procedures.
Object interaction without a prescribed order.(*)
Incorrect. Refer to Section 2 Lesson 3.
10. 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.
calculateDiameter()
calculateCircumference()
calculateArea()
radius(*)
color(*)
Incorrect. Refer to Section 2 Lesson 3.
11. In the code example below, identify any methods:
12. During the Design phase of software development, the programmer implements features
gathered during the Requirement phase.
True
False (*)
Incorrect. Refer to Section 2 Lesson 1.
13. Which of the following are adequate definitions for components of the Spiral Model of
Development?
Develop: Collect all specified instructions
Requirements: Start the development
Design: Plan the approach(*)
Test: Run the code and verify results(*)
Incorrect. Refer to Section 2 Lesson 1.
14. If the requirement step of the Spiral Model of development is forgotten, which of the
following could occur?
Code becomes messy.
The Program gives inaccurate results.
Solutions seem elusive.
Required software features are missing from the program. (*)
Incorrect. Refer to Section 2 Lesson 1.
int x = 25 - 5 * 4 / 2 - 10 + 4;
8
9 (*)
34
7
Correct
(1) Points
String firstName = “Alex”;(*)
double checkingAmount = 1500;(*)
int averageDollarAmount = 19.95;
boolean age = 20;
Correct
12. Java is a strongly typed language; therefore you must declare a data type for all
variables.
True (*)
False
Correct
int x = 25 - 5 * 4 / 2 - 10 + 4;
8
9 (*)
34
7
Correct
2. What is the range of the byte data type?
–27 to 27–1 (*)
–215 to 215–1
–231 to 231–1
–263 to 263–1
Correct
7. Assuming x is an int, which of the following are ways to increment the value of x by 1?
x = x +1;(*)
x = +1;
x+;
x++;(*)
x += 1;(*)
Correct
1. The Java compiler automatically promotes byte, short, and chars data type values to int
data type.
True (*)
False
Correct
4. A double with the value of 20.5 is cast to an int. What is the value of the int?
25
21
20 (*)
20.5
Correct
8. In Java, char is a primitive data type, while String is an object data type.
True (*)
False
Correct
9. The print() method prints to the console and automatically creates a line.
True
False (*)
Incorrect. Refer to Section 3 Lesson 3.
// Fragment 1
String inputString = JOptionPane.showInputDialog("??");
int input = Integer.parseInt(inputString);
input++;
// Fragment 2
int input = Integer.parseInt(JOptionPane.showInputDialog("??")) + 1;
True (*)
False
Correct
15. You write a statement that assigns a value to a String variable as shown below.
1. What is parsing?
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
2. A double with the value of 20.5 is cast to an int. What is the value of the int?
21
20.5
20 (*)
25
Correct
4. A short data type can be promoted to which of the following types?
double(*)
long(*)
int(*)
boolean
Correct
7. You write a statement that assigns a value to a String variable as shown below.
8. The Scanner class considers space as the default delimiter while reading the input.
True (*)
False
Correct
12. In Java, char is a primitive data type, while String is an object data type.
True (*)
False
Correct
1. Once an object is instantiated, how might its fields and methods be accessed in Java
o Using the comma(,) operator
o Using the double-colon(::) operator
o Using the dot(.) operator (*)
o Using the colon(:) operator
2. Which is a valid way of calling the testMethod in the TestClass? Assume a
testInstance has been created.
public void testMethod(int x, double y){
out.println(x/y);
}
o testMethod(3.5, 10);
o testMethod(3.5);
o testMethod(10, 3.5); (*)
o testMethod(10);
o testMethod(10, 3.5, 0);
3. Which of the following statements are true (Choose all correct answers)
o Methods cannot be written with parameters.
o Methods can be written with any number of parameters. (*)
o Parameter values can never be used within the method code block.
o Parameter values can be used within the method code block. (*)
o Methods can never be written with more than four parameters.
4. Object instantiation is done using what keyword?
o System
o instance
o new (*)
o void
5. Which of the following scenarios would be ideal for writing a method
o To group similar data types together
o When you don’t want to repeat similar lines of code to describe an
object’s behavior. (*)
o For every five to six lines of code.
o When you don’t find similar lines of code to describe an object’s behavior.
6. Methods allow all instance of a class to share same behaviors.
o True (*)
o False
7. An argument is a value that’s passed during a method call
o True (*)
o False
8. Which of the following two operations are appropriate for the main method (Choose
all correct answers)
o Creating instances of objects (*)
o Calling an instance object’s field and methods. (*)
o Assigning memory to the variables
o Calling local variables declared within a class’s method
9. Which of the following wild card character is used to import all the classes in a
particular package
o !
o ~
o ;
o * (*)
10. Which two are valid import statements of the Scanner class? (Choose all correct
answers)
o import java.*;
o import java.util.Scanner; (*)
o import java.util;
o import java.util.*; (*)
11. Which package is implicitly imported
o io
o lang (*)
o awt
o math
12. The JFrame and JOptionPane classes are in the javax.swing package. Which two
will import those classes? (Choose all correct answers)
o import javax.swing.J*;
o import javax.swing.*; (*)
o import javax.swing;
o import javax.swing.JOptionPane;
o import javax.swing.JFrame; (*)
13. The classes of the Java class library are organized into packages
o True (*)
o False
14. The import statement consists of two parts.
import package.className;
One is the package name and the other is the classname
o True (*)
o False
15. Which statement is true about packages
o A package doesn’t contain a group of related classes.
o Packages of the Java class library do not contain related classes.
o A package makes it difficult to locate the related classes.
o A package contains a group of related classes. (*)
3. The classes of the Java class library are organized into packages.
True (*)
False
Correct
7. Which of the following wild card character is used to import all the classes in a particular
package?
* (*)
!
~
;
Correct
9. 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 (*)
Incorrect. Refer to Section 4 Lesson 1.
This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Random
Number”.
1. Which class is used to generate random number?
a) java.lang.Object
b) java.util.randomNumber
c) java.util.Random
d) java.util.Object
View Answer
Answer: c
Explanation: java.util.random class is used to generate random numbers in java program.
2. Which method is used to generate boolean random values in java?
a) nextBoolean()
b) randomBoolean()
c) previousBoolean()
d) generateBoolean()
View Answer
Answer: a
Explanation: nextBoolean() method of java.util.Random class is used to generate random
numbers.
3. What is the return type of Math.random() method?
a) Integer
b) Double
c) String
d) Boolean
View Answer
Answer: b
Explanation: Math.random() method returns floating point number or precisely a double.
4. Random is a final class?
a) True
b) False
View Answer
Answer: b
Explanation: Random is not a final class and can be extended to implement the algorithm as
per requirement.
5. What is the range of numbers returned by Math.random() method?
a) -1.0 to 1.0
b) -1 to 1
c) 0 to 100
d) 0.0 to 1.0
View Answer
Answer: d
Explanation: Math.random() returns only double value greater than or equal to 0.0 and less
than 1.0.
6. How many bits are used for generating random numbers?
a) 32
b) 64
c) 48
d) 8
View Answer
Answer: c
Explanation: Random number can accept 64 bits but it only uses 48 bits for generating
random numbers.
7. What will be the output of the following Java code snippet?
int a = random.nextInt(15) + 1;
a) Random number between 1 to 15, including 1 and 15
b) Random number between 1 to 15, excluding 15
c) Random number between 1 to 15, excluding 1
d) Random number between 1 to 15, excluding 1 and 15
View Answer
Answer: a
Explanation: random.nextInt(15) + 1; returns random numbers between 1 to 15 including 1
and 15.
8. What will be the output of the following Java code snippet?
int a = random.nextInt(7) + 4;
a) Random number between 4 to 7, including 4 and 7
b) Random number between 4 to 7, excluding 4 and 7
c) Random number between 4 to 10, excluding 4 and 10
d) Random number between 4 to 10, including 4 and 10
View Answer
Answer: d
Explanation: random.nextInd(7) + 4; returns random numbers between 4 to 10 including 4
and 10. it follows “nextInt(max – min +1) + min” formula.
9. Math.random() guarantees uniqueness?
a) True
b) False
View Answer
Answer: b
Explanation: Math.random() doesn’t guarantee uniqueness. To guarantee uniqueness we
must store the generated value in the database and compare against already generated
values.
10. What is the signature of Math.random() method?
a) public static double random()
b) public void double random()
c) public static int random()
d) public void int random()
View Answer
Answer: a
Explanation: public static double random() is the utility method provided by Math class which
returns double.
1. Which class is used to generate random numbers?
Double
Number
Random (*)
Integer
Correct
2. You need to generate random integer values between 0 and 80 (inclusive). Which
statement should you use?
nextInt(81); (*)
nextInt(0-79);
nextInt(80);
nextInt();
Incorrect. Refer to Section 4 Lesson 4.
3. You need to generate random integer values in the range 2 through 10. This code
fragment will produce the desired result.
14. The Math class methods can be called without creating an instance of a Math object.
True (*)
False
Correct
9. The replaceFirst() method replaces only the first occurrence of matching character pattern
in a string.
True (*)
False
Correct
10. The indexOf() method returns the index value of a character in the string.
True (*)
False
Correct
11. String objects are immutable.
True (*)
False
Correct
12. You need to generate random integer values in the range 2 through 10. This code
fragment will produce the desired result.
13. You need to generate random integer values between 0 and 80 (inclusive). Which
statement should you use?
nextInt();
nextInt(0-79);
nextInt(81); (*)
nextInt(80);
Correct
2. A break statement causes control to transfer to the end of the switch statement.
True (*)
False
Correct
3. The switch statement is a more efficient way to write code when dealing with a large
range of unknown values.
True
False (*)
Incorrect. Refer to Section 5 Lesson 3.
Compilation error
Vowels
Vowels
Consonants (*)
Correct
int rating;
int experience;
if (rating > 1 && experience == 5) {
System.out.println (“Eligible for a bonus”);
}
5 rating and 1 experience
5 experience and 1 rating
5 experience and 2 or more rating (*)
Less than 5 experience and 1 rating.
Incorrect. Refer to Section 5 Lesson 2.
8. 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");
}
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
10. An if/else statement is used when you need to choose between two alternatives.
True (*)
False
Correct
13. The equal sign (=) is used to make an assignment, whereas the == sign merely makes a
comparison and returns a boolean.
True (*)
False
Correct
14. Which operator is used to test if both sides of a boolean expression are equal?
=
>=
== (*)
<=
Correct
1. An if/else statement is used when you need to choose between two
alternatives.
True (*)
False
Correct
switch statement(*)
for loop
do while loop
if/else statement(*)
Incorrect. Refer to Section 5 Lesson 1.
+(*)
%(*)
||
Correct
13. A break statement causes control to transfer to the end of the switch
statement.
True (*)
False
Correct
Failed
A
Congratulations!
Congratulations! Good Work Average Barely Passing Failed (*)
Incorrect. Refer to Section 5 Lesson 3.
Langsung saja check this out soal dan jawaban Mid Term – Java Foundation-nya!
16. Which two statements are true about type casting? (Choose all correct answers)
o Type casting cannot be performed on equations.
o Type casting retains the size of the value or the original data type.
o Type casting changes the type of the value stored. (*)
o Type casting lowers the range of possible values. (*)
17. Which is a valid way to parse a String as an int?
o int intVar1 = Integer.parseInt(“100”); (*)
o nt intVar1 = (int)”100″;
o int intVar1 = Integer.parseInt(“One Hundred”);
o int intVar1 = “100”;
18. Which exception occurs because a String cannot be parsed as an int?
o NumberFormatException (*)
o ArithmeticException
o NullPointerException
o ValueNotFoundException
19. in readies Scanner to collect input from the console.
o True (*)
o False
20. The Scanner class accepts input in which form?
o Integer
o Callables
o Tokens (*)
o Future
45. The equal sign (=) is used to make an assignment, whereas the == sign merely makes a
comparison and returns a boolean.
o True (*)
o False
46. A String comparison with == compares the Strings’ locations in memory and not the content
of the String.
o True (*)
o False
47. What is the output?
public static void main(String[] args) {
int age = 43;
if (age == 43){
out.print(“Bob is 43 “);
}
if (age == 50){
System.out.print(“Bob is 50 “);
}
}
o Bob is 43 Bob is 50
o Bob is 50
o No output
o Bob is 43 (*)
48. Which are used in a boolean expression? (Choose all correct answers)
o Variables (*)
o Loops
o Operators (*)
o Errors
49. A break statement causes control to transfer to the end of the switch statement.
o True (*)
o False
50. Which two of the following data types can be used in a switch statement? (Choose all correct
answers)
o boolean
o String (*)
o float
o int (*)