Prog 2 NDQ

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

To declare a method as static, place the keyword static before ________ in the method’s

declaration.

a.
the argument list

b.
the method name

c.
the method modifier

d.
the return type

For the code segment below:

switch(q) {
case 1:
System.out.println("apple");
break;
case 2:
System.out.println("orange");
break;
case 3:
System.out.println("banana");
break;
case 4:
System.out.println("pear");
case 5:
System.out.println("grapes");
default:
System.out.println("kiwi");
}

Which of the following values for q will result in kiwi being included in the output?

a.
2.

b.
1.

c.
Any integer less than 1 and greater than or equal to 4.

d.
3.

Java requires a ________ call for every object that’s created.

a.
parameterless

b.
destructor

c.
parameterized

d.
constructor

Java requires a ________ call for every object that’s created.


a.
parameterless

b.
destructor

c.
parameterized

d.
constructor

If a class does not define constructors, the compiler provides a default constructor with no
parameters, and the class’s instance variables are initialized to ________.

a.
false

b.
null

c.
zero

d.
their default values.

Which of the following statements is true?

a.
Constructors can specify parameters but not return types.

b.
Constructors can specify parameters and return types.
c.
Constructors cannot specify parameters but can specify return types.

d.
Constructors can specify neither parameters nor return types.

The following code displays ___________.

double temperature = 50;

if (temperature >= 100)


System.out.println("too hot");
else if (temperature <= 40)
System.out.println("too cold");
else
System.out.println("just right");

a.
too cold

b.
too hot too cold just right

c.
too hot

d.
just right

Which of the following is true?


a.
Pseudocode is not an actual computer programming language.

b.
Pseudocode is used to describe executable statements that will eventually be translated by the
programmer into a program.

c.
Pseudocode is used to describe an algorithm.

d.
All of the above.

Which of the following operators associates from left to right?

a.
%=

b.
=

c.
?:

d.
/

Any field declared with keyword ________ is constant.

a.
static

b.
constant
c.
final

d.
const

The format specifier ________ is a placeholder for an int value.

a.
%n

b.
%d

c.
%int

d.
%s

What is the result value of c at the end of the following code segment?
int c = 8;
c++;
++c;
c %= 5;

a.
3.

b.
None of the above.
c.
1.

d.
0.

To exit out of a loop completely, and resume the flow of control at the next statement after
the loop, use a _______.

a.
return statement.

b.
break statement.

c.
Any of the above.

d.
continue statement.

What is output by the following Java code segment?


int temp = 200;

if (temp > 90) {


System.out.println("This porridge is too hot.");
}

if (temp < 70) {


System.out.println("This porridge is too cold.");
}
if (temp == 80) {
System.out.println("This porridge is just right!");
}

a.
This porridge is too cold.

b.
This porridge is just right!

c.
This porridge is too hot.

d.
None of the above.

The software that contains the core components of the operating system is the ________.

a.
kernel.

b.
colonel.

c.
central processing unit

d.
core
What is the value of result after the following Java statements execute (assume all variables
are of type int)?
a = 4;
b = 12;
c = 37;
d = 51;
result = d % a * c + a % b + a;

a.
127

b.
59

c.
119

d.
51

An overloaded method is one that ________.

a.
has the same name and parameters, but a different return type as another method

b.
has the same name as another method, but different parameters (by number, types or order
of the types)

c.
has the same name and parameters as a method defined in another class

d.
has a different name than another method, but the same parameters

Optional parentheses in expressions are said to be

a.
redundant.

b.
binary operators.

c.
implied.

d.
declared.

What is y displayed in the following code?

public class Test1 {


public static void main(String[] args) {
int x = 1;
int y = x = x + 1;
System.out.println("y is " + y);
}
}

a.
y is 0.

b.
The program has a compile error since x is redeclared in the statement int y = x = x + 1.

c.
y is 2 because x + 1 is assigned to x and then x is assigned to y.

d.
y is 1 because x is assigned to y first.

Which statement below is false?

a.
Structured programming produces programs that are easier to test.

b.
Structured programming promotes simplicity.

c.
Structured programming produces programs that are easier to modify

d.
Structured programming requires four forms of control.

What is output by the following Java code segment?


int temp = 180;

if (temp > 90) {


System.out.println("This porridge is too hot.");

// cool down
temp = temp – (temp > 150 ? 100 : 20);
}
else {
if (temp < 70) {
System.out.println("This porridge is too cold.");

// warm up
temp = temp + (temp < 50 ? 30 : 20);
}
}

if (temp == 80) {
System.out.println("This porridge is just right!");
}

a.
This porridge is too hot.

b.
This porridge is too cold.
This porridge is just right!

c.
This porridge is just right!

d.
None of the above.

Which of the following can be an argument to a method?

a.
Constants.

b.
All of the above.
c.
Variables.

d.
Expressions.

What is output by the following Java code segment?


int temp = 180;

while (temp != 80) {


if (temp > 90) {
System.out.print("This porridge is too hot! ");

// cool down
temp = temp – (temp > 150 ? 100 : 20);
}
else {
if (temp < 70) {
System.out.print("This porridge is too cold! ");

// warm up
temp = temp + (temp < 50 ? 30 : 20);
}
}
}

if (temp == 80) {
System.out.println("This porridge is just right!");
}
a.
This porridge is too hot! This porridge is just right!

b.
None of the above.

c.
This porridge is too cold! This porridge is just right!

d.
This porridge is just right!

Analyze the following code.

boolean even = false;


if (even) {
System.out.println("It is even!");
}

a.
The code displays It is even!

b.
The code displays nothing.

c.
The code is wrong. You should replace if (even) with if (even = true).

d.
The code is wrong. You should replace if (even) with if (even == true).
A static method can ________.

a.
manipulate only static fields in the same class directly

b.
All of the above.

c.
call only other static methods of the same class directly

d.
be called using the class name and a dot (.)

How many times is the body of the loop below executed?


int counter = 1;

while (counter > 20) {


// body of loop
counter = counter - 1;
}

a.
21.

b.
19.

c.
20.

d.
0.

Which of the following statements does not alter the value stored in a memory location?

a.
y = y + 2;

b.
width = Integer.parseInt(input);

c.
int a;

d.
number = 12;

Declaring instance variables ________ is known as data hiding or information hiding.

a.
masked

b.
secure

c.
static

d.
private
Which of the following code segments does not increment val by 3:

a.
All of the above increment val by 3.

b.
c = 3;
val = val + (c == 3 ? 2 : 3);

c.
val += 3;

d.
val = val + 1;
val = val + 1;
val = val + 1;

Which of the following languages is used primarily for scientific and engineering
applications?

a.
COBOL.

b.
Fortran.

c.
Basic.

d.
Pascal.
The empty statement is denoted by what symbol?

a.
This porridge is too cold.
This porridge is just right!
b. Parentheses ()

b.
Colon :

c.
Semicolon ;

d.
Braces {}

A key part of enabling the JVM to locate and call method main to begin the app’s execution
is the ________ keyword, which indicates that main can be called without first creating an
object of the class in which the method is declared.

a.
public

b.
static

c.
private

d.
stable
In Java, the word true is ________.

a.
a Boolean literal

b.
a Java keyword

c.
same as value 1

d.
same as value 0

What is the value of the following expression?


true || true && false

a.
false

b.
true

You must call most methods other than ________ explicitly to tell them to perform their tasks.

a.
static methods

b.
public methods

c.
main

d.
private methods

The equal comparison operator in Java is __________.

a.
==

b.
<>

c.
^=

d.
!=

Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.

a.
A1

b.
66

c.
B

d.
Illegal expression
What is y after the following switch statement is executed?

int x = 3; int y = 4;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}

a.
0

b.
1

c.
4

d.
3

e.
2

Suppose income is 4001, what is the output of the following code?

if (income > 3000) {


System.out.println("Income is greater than 3000");
}
else if (income > 4000) {
System.out.println("Income is greater than 4000");
}

a.
Income is greater than 4000

b.
Income is greater than 3000

c.
no output

d.
Income is greater than 4000 followed by Income is greater than 3000

e.
Income is greater than 3000 followed by Income is greater than 4000

Analyze the following code:

boolean even = false;


if (even = true) {
System.out.println("It is even");
}

a.
The program runs fine and displays It is even.

b.
The program has a compile error.

c.
The program has a runtime error.

d.
The program runs fine, but displays nothing.

The format specifier ________ is used to output values of type float or double.

a.
%fd

b.
%d

c.
%f

d.
%r

For the two code segments below:


Segment A
int q = 5;
switch(q) {
case 1:
System.out.println(1);
case 2:
System.out.println(2);
case 3:
System.out.println(3);
case 4:
System.out.println(4);
case 5:
System.out.println(5);
default:
System.out.println("default");
}

Segment B
q = 4;
switch(q) {
case 1:
System.out.println(1);
case 2:
System.out.println(2);
case 3:
System.out.println(3);
case 4:
System.out.println(4);
case 5:
System.out.println(5);
default:
System.out.println("default");
}

Which of the following statements is true?

a.
The output for Segment A is:
5
default

b.
The output for Segment B is:
4

c.
The output for Segment A is:
default

d.
The output for Segment B is:
45 default

Which of the following is not one of the three general types of computer languages?

a.
Assembly languages.

b.
Machine languages.

c.
Spoken languages.

d.
High-Level languages.

Each of the following is a relational or equality operator except:

a.
=!

b.
==
c.
<=

d.
>

Local variables must be ________.

a.
declared and initialized in two steps.

b.
declared at the top of the method’s body.

c.
initialized before their values are used in an expression.

d.
initialized when they're declared.

Which expression is equivalent to if (!(grade == sentinelValue))?

a.
if (grade !== sentinelValue)

b.
if (grade != sentinelValue)

c.
! if (grade !== sentinelValue)
d.
! if (grade == sentinelValue)

The "less than or equal to" comparison operator in Java is __________.

a.
!=

b.
=<

c.
<<

d.
<

e.
<=

Types in Java are divided into two categories. The primitive types are boolean, byte, char, short,
int, long, float and double. All other types are ________ types.

a.
declared

b.
static

c.
source

d.
reference
Overloaded methods always have the same _________.

a.
number of parameters

b.
method name

c.
return type

d.
order of the parameters

Where can local variables declared within a method’s body be used?

a.
Only in that method between the line in which they were declared and the closing brace of
that method.

b.
Anywhere within the class.

c.
Only within while or if statements within the method in which they were declared.

d.
Same as (a), but not within while or if statements.

Which of the following statements is true?

a.
Compilers translate high-level language programs into machine language programs.

b.
Interpreter programs typically use machine language as input.

c.
None of the above.

d.
Interpreted programs run faster than compiled programs.

You might also like