Java MCQ With Answers - 1
Java MCQ With Answers - 1
Inheritance
Encapsulation
Polymorphism
Parallelism ******
machine code.
Assembly code.
Byte code. ******
JVM code.
void
int
Integer
static ******
7. What is the output of the following code if the input string is "CS 180"?
Scanner scanner = new Scanner();
String str;
str = scanner.next();
System.out.print(str);
(a)
(b)
(c)
(d)
CS180
CS
CS 180
The above code fragment does not compile. ******
9. Which of the following is TRUE about the piece of JAVA code below if we judge the statements
independently?
final double PI;
int radius = Integer.parseInt("3.5");
double area = Math.PI * Math.pow(radius, 2);
(a)
(b)
(c)
(d)
The third statement computes the area of a circle whose radius is in variable radius. ******
The first statement correctly declares the constant PI.
The second statement correctly obtains the radius from the given string.
The third statement incorrectly uses Math.pow.
1
6
5 ******
4
12. What is the range of the random number r generated by the code below?
int r = (int)(Math.floor(Math.random() * 8)) + 2;
(a)
(b)
(c)
(d)
3
3
2
2
<=
<=
<=
<=
r
r
r
r
<=
<=
<=
<=
10
9
10
9 ******
13. If a local variable of a method shop() belonging to a class called Walmart has the same name as
a data member of Walmart, which value is used when shop() is executing?
(a)
(b)
(c)
(d)
short
average short
tall short ******
tall
n
n
n
n
n
=
=
=
=
=
n
n
n
n
n
+
+
+
+
+
1;
2;
3;
4;
5;
n
p
q
r
=
=
=
=
20;
n + 5;
p - 10;
2 * (p - q);
switch(n)
{
case p: n = n + 1;
case q: n = n + 2;
case r: n = n + 3;
default:
n = n + 4;
}
(a)
(b)
(c)
(d)
24
This code does not compile. ******
20
27
18. What is the value of variable z after executing the following code?
int x = 5;
int y = 5;
int z = 5;
if (x > 3)
if (y > 4)
if (z > 5)
z += 1;
else
z += 2;
else
z += 3;
z += 4;
(a)
(b)
(c)
(d)
9
5
11 ******
7
7.5
7.0
5.0
None of the above ******
a;
b = new String();
c = "";
d = "null";
a ******
b
c
d (this answer also accepted)
Number
Million
Billion
Trillion
Quadrillion
Quintillion
Sextillion
Nonillion
Googol
You do not need to worry about special ordinal numbers such as 1st, 2nd, 3rd, and so on. That is, you
can use "th" for all ordinal numbers. For example:
10 raised to the 21th power is a sextillion.
There is no single word for 10 raised to the 2th power.
Note: You MUST USE the switch statement. Write your code on the next page.
2. (20 points) Define a class called Triangle with three integer data members a, b, and c as the
lengths of its three edges. This class should have the following methods:
(a) a constructor with 3 parameters representing the 3 edges
(b) a method isTriangle() which returns true if the 3 edges are all positive and they satisfy the triangle
inequality where a+b > c, a+c > b, b+c > a.
(c) a method getAngle() with 1 parameter, an edge, which returns the angle in degrees of the angle
facing the given edge.
The signature of these methods are given below:
public Triangle(int newa, int newb, int newc)
public boolean isTriangle()
public double getAngle(int edge)
Note: getAngle() should return zero if the triangle is not really a triangle. Also, here are a few formulas to help you define the class:
FYI, if A is the angle facing side a, then the following formula should help:
cosA =
b2 + c 2 a 2
2bc
Note: Write your code on the next page. It must be a complete class.
10
3. (20 points) For this question you are provided with the class Student that has at least three methods
available to you. getScore() returns the students score. isAttendanceGood() returns true
if the student has good attendance, false otherwise. isTalkative() returns true if the student is
talkative in class, false otherwise. The signature of these methods are given below:
public int getScore()
public boolean isAttendanceGood()
public boolean isTalkative()
Note that the class may have more data members or methods that are not reflected here. You are asked
to write a method printGrade with the following signature:
void printGrade(Student s1);
As the name suggests, this method takes an object of type Student and prints out the final grade for
that student. The grade is assigned following these guidelines.
(a)
(b)
(c)
(d)
(e)
11
12