Java Questions

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

1. The purpose of programs is to _____?

Allow people to download software


Provide instructions to a computer to enable it to perform tasks
Provide a career path for software engineers
Disrupt the world

2. What kind of language is a computer able to


understand?
Any programming language
Only machine language
Only Java
Only human-friendly programming languages

3. What is an algorithm?
A complex problem
A guide that provides the high level steps to complete a task
A programming language
A coffee flavor

4. Programs are implementations of ____?


Algorithms
Computers
Machine code
0s and 1s
1. Which of the following serves as a folder to organize Java
files?
class

package

method

variable

2. Which of the following represents a Java file?


class

package

method

variable

3. Which of the following is a memory location used to store


data?
class

package

method

variable
4. What’s the name of the method that is invoked first when
a Java class is executed?
main

first

default

priority

5. Which of the following package names violates Java


convention?
MyPackage

mypackage

mypackage1

my_package

6. Which of the following class names follows Java


convention?
MYCLASS

myClass

MyClass

myclass
7. Which of the following variable names is invalid?
name1

nameOne

1name

name_one

8. Which of the following is a reserved word in Java?


public

print

scanner

hours

9. Which of the following encloses a block of statements?


Brackets - []

Curly braces - {}

Parentheses - ()

Semicolon - ;

10. Which of the following denotes a comment in Java?


%%

..

//

##
11. Which statement will print HELLO to the console?
print.out(“HELLO”):

System.out.println(“HELLO”);

Scanner.in(“HELLO”).

Out(“HELLO”),

12. What is the purpose of the semicolon in Java?


To enclose a block of statements

To declare a new variable

To end a statement

To see all available methods

13. Which of the following is a data type suitable for whole


numbers?
double

int

nextInt

nextDouble
14. Which of the following is a data type suitable for decimal
numbers?
double

int

nextInt

nextDouble

15. Which of the following is a data type suitable for text?


String

int

double

nextString

16. Which operator is used to multiply numbers in Java?


+

x
17. Which operator is used to append a variable’s data to a
String?
+

18. Given you have a variable named output with a data type
of String, which of the following is a valid assignment for
this variable?
“Hello World” = String output;

“Hello World” = output String;

String output = “Hello World”;

output = String “Hello World”;

19. If you type something within your program that is


invalid, you will get a _____?
compilation error

bug report

runtime error

warning alert
20. Java is case-sensitive.
True

False

1. Which of the following is used to enclose code


comments that span multiple lines?
//
/* */
{}
[]

2. Comments can only exist within a method.


True
False

3. What is an if statement?
A decision structure that executes statement(s) given some condition is met
A loop
A variable
A class

4. What goes inside of the parentheses of an if


statement?
A counter
A variable declaration
A condition
A String
5. A condition is what type of expression?
String
int
float
boolean

6. The boolean data type holds ____


whole numbers
decimal numbers
Strings
true or false

7. Any time a variable is updated with a new


value, it must be re-declared.
True
False

8. What is a magic number?


A number that the program randomly generates
A number that is hard-coded as opposed to being represented by a variable
A number that changes every time the program runs
A number with the data type of Magic
1. How many paths exist in an if-else statement?
0
1
2
3

2. When will statements inside of an if block be


executed?
All the time
Only if condition is true
Only if condition is false
Never

3. When will statements inside of an else block be


executed?
All the time
Only if condition is true
Only if condition is false
Never

4. It is possible to execute the statements of both


the if and the else blocks of an if-else statement.
True
False
5. When can curly braces be omitted from an if
statement?
When there’s only one statement that needs to be executed
When there are several statements that need to be executed
Anytime - it’s completely optional
Never - it’s always required

1. An if statement can exist inside of another if


statement.
True
False

2. The double data type can hold decimal and


whole numbers.
True
False

3. Nested ifs have the same effect as multiple


stand-alone if statements.
True
False

4. Nested ifs have the same effect as a single if-


else statement.
True
False
5. Within a class, how does Java know which else
statement is meant to correspond to which if
statement.
The corresponding else statement is the very next else statement that follows a
completed if statement
There cannot be multiple if and else statements within a single program
The corresponding if and else statements are given the same name
Java has no way of knowing

6. When will a nested if statement be evaluated?


When the if statement in which it is nested inside of is false
When the if statement in which it is nested inside of is true
The order is random and cannot be controlled by the programmer
Before the if statement in which it is nested inside of

1. When is it ideal to use an if-else-if statement?


When there is one possible path
When there are two possible paths
When there are more than two possible paths

2. With if-else-if statements, multiple conditions


must be specified.
True
False
3. Which branches of an if-else-if statement are
executed?
All that are true
The first one that is true
All that are false
The last one that is false

4. Which of the following is a valid value for the


char data type?
2
3.5
“Hello World”
‘Z’

5. When declaring a variable, you are required to


also initialize it with a value within the same
declaration statement.
True
False
1. When is it ideal to use a switch statement?
When there is one possible path
When there are two possible paths
When there are more than two possible paths

2. Which decision structure is interchangeable


with the switch statement?
if
if-else
if-else-if
nested ifs

3. Which type of expression does the condition of


a switch statement evaluate?
Any
None
Equality
Greater or less than

4. Each path of a switch statement is denoted by


the word _____
case
switch
if
break
5. Which cases of a switch statement are
executed?
All that are true
The first one that is true
All that are false
The last one that is false

6. The purpose of the reserved word, break, is to


Exit an if block and enter an else block
End a case block within a switch statement
Denote the end of the nested if statements
Close the input stream

7. When is the default case within a switch


statement executed?
Before all other cases are executed
When there are no case statements present
When the value being evaluated does not equal any of the case statements
After a case is executed

8. What will happen in a case where the break


statement is omitted?
The next case will also be executed
The program will crash
There will be a compilation error
The program will add it by default
9. Which decision structure is best for a case
where there’s only one conditional path needed?
if
if-else
if-else-if
nested ifs
switch

10. Which decision structure is best when there’s


two conditional paths, where one should be
executed if the condition is true and the other if
the condition is false?
if
if-else
if-else-if
nested ifs
switch

11. Which decision structure is used when there


are more than two conditional paths and its
condition only checks for equality?
if
if-else
if-else-if
nested ifs
switch
12. Which decision structure can be used when
you need to immediately check another
condition if the first condition was met?
if
if-else
if-else-if
nested ifs
switch

1. Which relational operator is used to test for


equality?
>
<
=
==

2. Which of these is an appropriate way to test if


a equals 5?
if(a = 5)
if(a == 5)
if(a.equals(5))
if(5 = a)

3. Which is true of logical operators?


They are interchangeable with relational operators
They are used to combine two conditions to result in a single boolean value
They are data types
There are an infinite number of them to choose from
4. Which of the following is the logical operator
for AND
>>
||
&&
==

5. Which of the following is the logical operator


for OR
>>
||
&&
==

6. Which logical operator is used to negate a


boolean expression?
==
!=
!
&&

7. What’s the value of (2>3 && 4<=4)?


True
False

8. What’s the value of (9 != 0 || 5>5)?


True
False
9. Considering short circuit logic, how many
conditions will actually be evaluated in the
expression (1>2 && 4<5)?
1
2
4
5

1. What is the purpose of loops?


To repeat a block of code more than once
To provide multiple paths of execution based on conditions
To make your code dizzy
To draw circles with your code

2. Which of the following is NOT a loop


structure?
While
Do While
If-else
For

3. Which of the following are condition-


controlled loops?
While and Do While
For and While
For and Do While
Any nested loop
4. Which of the following is a count-controlled
loop?
While
Do While
If-else
For

5. Which loop should be used if you know the


number of iterations it needs to execute?
While
Do While
For
Ring Around the Rosie

6. Which loop is best to use if the contents


definitely need to be executed at least once?
While
Do While
For
Singleton

7. Which loop is best to use if the loop may or


may not need to run at all?
While
Do While
For
Break
8. Proper use of a ________ helps avoid an infinite
loop?
Sentinel
Semicolon
Curly brace
Bracket

9. Which of the following is an example of a


statement that would increment a sentinel by 1?
i++
i--
i+
i*

10. Which of the following is NOT one of the


statements required in a for loop’s definition?
Initialization on sentinel
Loop Condition
Incrementer / Decrementer
Loop Name

1. The first line of a method is called a _______


header
opening
definition
body
2. Which of the following is NOT a required part
of a method declaration?
name
return type
access modifier
parentheses

3. Which of these is NOT a part of a method’s


signature?
name
parameter list
non-access modifier

4. How do you call a method?


By its name
By its data type
By its return type
By its access modifier

5. What determines the order in which methods


are executed within a class?
The order in which they are defined within the class
The order in which they are called
Alphabetical order
It's random
6. Which is the reserved word to end a method
and send back a value to the caller?
exit
break
return
close

7. What type of data will a method with the


`void` return type return?
String
int
boolean
none

8. What is the scope of a method's parameter?


Can be used anywhere in the class
Can be used anywhere within the method
Can be used only within the method's header
Cannot be used within other blocks within the method, such as loops and decisions

9. Which of these would be a valid return type?


public
static
String
main
10. How many values can a method return?
1
2
3
Unlimited

11. If a called method returns a value, the calling


method is required to receive what’s returned
and store it into a variable.
True
False

12. A calling method’s argument list must be in


the exact same order as the receiving method’s
parameter list.
True
False

13. Arguments and their corresponding


parameters must have the exact same name.
True
False

14. Variables declared at the class level are also


known as _____ variables.
method
global
local
conditional
15. Variables declared at the method level are
also known as _____ variables.
method
global
local
conditional

You might also like