0% found this document useful (0 votes)
120 views30 pages

PROGJAVAPROG3112

The document contains multiple choice questions about Java programming concepts. It asks what the value of y would be after a switch statement is executed, which of the given options is not a type of computer language, and which keyword is used to declare instance variables as private. It then provides multiple choice answers to select for each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views30 pages

PROGJAVAPROG3112

The document contains multiple choice questions about Java programming concepts. It asks what the value of y would be after a switch statement is executed, which of the given options is not a type of computer language, and which keyword is used to declare instance variables as private. It then provides multiple choice answers to select for each question.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

PROG (JAVA) 3112

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.
2

b.
3

c.
0

d.
4

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

a.
High-Level languages.
b.
Spoken languages.

c.
Assembly languages.

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

a.
masked 

b.
private 

c.
secure 

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

a.
same as value 1

b.
a Boolean literal 

c.
same as value 0

d.
 a Java keyword
Analyze the following code.
boolean even = false;
if (even) {
  System.out.println("It is even!");
}

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

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

c.
The code displays It is even!

d.
The code displays nothing.
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.
private

c.
static

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

a.
number = 12;

b.
y = y + 2;

c.
width = Integer.parseInt(input);

d.
int a;
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.
no output

b.
Income is greater than 4000

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

d.
Income is greater than 3000

e.
Income is greater than 3000 followed by Income is greater than 4000
Where can local variables declared within a method’s body be used?
a.
Same as (a), but not within while or if statements.

b.
Anywhere within the class.

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

d.
Only within while or if statements within the method in which they were declared.
The format specifier ________ is used to output values of type float or double. 

a.
%d

b.
%fd

c.
%r

d.
%f
Each of the following is a relational or equality operator except:

a.
   >

b.
<=

c.
=!

d.
==
Overloaded methods always have the same _________.

a.
order of the parameters

b.
number of parameters

c.
return type

d.
method name
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.
51

c.
59

d.
119
Optional parentheses in expressions are said to be
a.
implied.

b.
binary operators.

c.
declared. 

d.
redundant.
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.
static 

b.
declared

c.
source

d.
reference
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 2 because x + 1 is assigned to x and then x is assigned to y.

b.
y is 0.

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

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 requires four forms of control.

c.
Structured programming produces programs that are easier to modify

d.
Structured programming promotes simplicity.
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.

a.
A1

b.
 B

c.
Illegal expression
d.
66
An overloaded method is one that ________.

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

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

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

d.
has the same name as another method, but different parameters (by number, types or order
of the types)
The software that contains the core components of the operating system is the ________.

a.
kernel.

b.
core

c.
colonel.

d.
central processing unit
To declare a method as static, place the keyword static before ________ in the method’s
declaration.

a.
the argument list

b.
the return type

c.
the method name

d.
the method modifier
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.
1.

c.
0.

d.
None of the above.
Local variables must be ________.

a.
declared and initialized in two steps.

b.
initialized when they're declared.

c.
initialized before their values are used in an expression.
d.
declared at the top of the method’s body.
You must call most methods other than ________ explicitly to tell them to perform their tasks.

a.
private methods

b.
static methods

c.
public methods

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

a.
%d

b.
%int

c.
%s

d.
%n
Which of the following can be an argument to a method?

a.
Expressions.

b.
All of the above.
c.
Constants.

d.
Variables.
Which of the following statements is true?

a.
Constructors can specify neither parameters nor return types.

b.
Constructors can specify parameters and return types.

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

d.
Constructors can specify parameters but not return types.
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 equal comparison operator in Java is __________.

a.
^=
b.
<>

c.
!=

d.
==
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 cold.
This porridge is just right! 

b.
This porridge is too hot.

c.
None of the above.

d.
This porridge is just right!
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.
their default values.

b.
null

c.
zero

d.
false
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:  
45 default

c.
The output for Segment A is:  
default

d.
The output for Segment B is: 
4
Analyze the following code:

boolean even = false;


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

a.
The program runs fine, but displays nothing.

b.
The program has a compile error.

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

d.
The program has a runtime error.
Which of the following code segments does not increment val by 3:
a.
val = val + 1;
val = val + 1;
val = val + 1;

b.
val += 3;

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

d.
 All of the above increment val by 3.
Which of the following languages is used primarily for scientific and engineering applications?

a.
COBOL.

b.
Pascal.

c.
Basic.

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

a.
!=

b.
<<

c.
<

d.
<=

e.
=<
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.
None of the above.

c.
This porridge is too hot.

d.
This porridge is just right!
How many times is the body of the loop below executed?
int counter = 1;

while (counter > 20) {


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

a.
0.

b.
20.

c.
21.

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

a.
destructor

b.
parameterless

c.
parameterized

d.
constructor
A static method can ________.

a.
All of the above.

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

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

d.
manipulate only static fields in the same class directly
To exit out of a loop completely, and resume the flow of control at the next statement after
the loop, use a _______.

a.
break statement.

b.
return statement.

c.
Any of the above.

d.
continue statement.
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.
just right

b.
too cold

c.
too hot

d.
too hot too cold just right
Which of the following statements is true?

a.
Interpreted programs run faster than compiled programs.

b.
None of the above.

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

d.
Interpreter programs typically use machine language as input.
What is the value of the following expression?
true || true && false
a.
false

b.
true
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!
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.
3. 

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

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

a.
Colon :

b.
Braces {}

c.
Semicolon ;

d.
This porridge is too cold.
This porridge is just right! 
b. Parentheses ()
Any field declared with keyword ________ is constant.

a.
static

b.
const

c.
final

d.
constant
Which of the following is true? 

a.
Pseudocode is not an actual computer programming language.

b.
Pseudocode is used to describe an algorithm.

c.
All of the above.

d.
Pseudocode is used to describe executable statements that will eventually be translated by
the programmer into a program.
Which of the following operators associates from left to right?

a.
/

b.
%=

c.
=

d.
?:

You might also like