Font size
WorksheetsJava Final exam (comprog2)
Total questions: 114
Worksheet time: 19mins
A while loop carries on as long as...
The condition in the brackets is true
The condition in the brackets is false
i < 10
the for loop has not ended
A while loop has the following format:
while (condition)
{
...
}
while
{
...
}
continue while (condition)
{
...
}
while [condition]
[
...;
]
OUTPUT?
int i = 1;
while (i < 5) {
System.out.println("Hello");
i++;
}
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
How many stars are printed?
7
3
4
5
Infinite Loop
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
How many times does the loop print a *
40
20
24
30
Infinite Loop
What does the following code print?
A rectangle of 8 rows with 5 stars per row
A rectangle of 8 rows with 4 stars per row
A rectangle of 6 rows with 5 stars per row
A rectangle of 6 rows with 4 stars per row
Infinite Looo
What does the following code print?
A rectangle of 9 rows with 5 stars per row
A rectangle of 6 rows with 6 stars per row
A rectangle of 7 rows with 5 stars per row
A rectangle of 7 rows with 6 stars per row
Infinite Loop
What does the following code print?
5 6 7 8 9
4 5 6 7 8 9 10 11 12
3 5 7 9 11
3 4 5 6 7 8 9 10 11 12
How many times does the following method print a *?
9
6
7
10
1 2 4 8 16 32 64
1 2 4 8 16 32
2 4 8 16 32 64
2 4 8 16 32
4 8 16 32 64
200 66 22 7 2
66 22 7 2
200 66 22 2
200 66 22
7
10
15
17
22
25
2
5
7
9
13
20
-1 0 2 5
-1 0 3 7
-1 1 4 8
0 2 5 9
0 1 3 7
0.0
-4.0
5
The program will end successfully, but nothing will be printed.
Nothing will be printed. Run-time error: ArithmeticException.
What will the following program segment display?
for(int a = 2; a <= 10; a += 3)
System.out.print(a+””);
2 5 8
2 5
2 5 8 11
2
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
_________________ is a collection of elements used to store the same type of data.
Array
Switch
Case
Loop
What is returned by values[5]?
2
12
6
8
var nums =[2, 3, 5, 8, 9, 11];
How would you access the fourth element in nums
nums[4]
nums[3]
nums(4)
nums(3)
X is ?
Array
Variable
X is ?
Array
variable
............. is a placeholder for only one data?
Array
Variable
.......... is a placeholder for related data?
Array
variable
which option represent: Array holding a String data?
var nums =[2, 3, 5, 8, 9, 11];
How would you access the first element in nums
num(1);
num[1];
num(0);
num[0];
x is array of 10 items, to access the last index ?
x[10];
x(10);
x[9];
x(9);
using (appendItem) will add the new item to the ........ of the array
First
Middle
End
Entire array is enclosed in:
( )
{ }
[ ]
A while loop has the following format:
while (condition)
{
...
}
while
{
...
}
continue while (condition)
{
...
}
while [condition]
[
...;
]
OUTPUT?
int i = 1;
while (i < 5) {
System.out.println("Hello");
i++;
}
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
How many stars are printed?
7
3
4
5
Infinite Loop
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
How many times does the loop print a *
40
20
24
30
Infinite Loop
What does the following code print?
A rectangle of 8 rows with 5 stars per row
A rectangle of 8 rows with 4 stars per row
A rectangle of 6 rows with 5 stars per row
A rectangle of 6 rows with 4 stars per row
Infinite Looo
What does the following code print?
A rectangle of 9 rows with 5 stars per row
A rectangle of 6 rows with 6 stars per row
A rectangle of 7 rows with 5 stars per row
A rectangle of 7 rows with 6 stars per row
Infinite Loop
What does the following code print?
5 6 7 8 9
4 5 6 7 8 9 10 11 12
3 5 7 9 11
3 4 5 6 7 8 9 10 11 12
How many times does the following method print a *?
9
6
7
10
Bank tom = new Bank(5000.0);
Which is the class?
Bank tom = new Bank(5000.0);
Which is the object?
which package is imported implicitly?
java.applet
java.util
java.lang
java.io
Classes in Java are:
Abstract data types, used as a template of objects
Where students learn
Dungeons and Dragons Character types
None of these
Which is the fundamental unit of OOP?
Object
Class
Class is a_________.
object / Template
blue print / Template
Instance / blue print
<class name><Object name>=new <class name>();
This is syntax of creating ______.
class
object
method
function
TextBook English=new Textbook();
In the above snippet, TextBook is a _______.
Object
Data member
Member function
Class
TextBook English=new Textbook();
In the above snippet, English is an _______.
Object
Class
Member function
Class name
The process of binding data and functions is called as
Inheritance
Polymorphism
Abstraction
Encapsulation
The process of hiding the implementation details from the user is called
Abstraction
Polymorphism
Inheritance
Encapsulation
A class can have any number of objects
false
true
neither true nor false
cannot say
The OOPs concept, exposing only necessary information to users or clients is known as
Inheritance
Insulation
Abstraction
Polymorphism
Predict the output of following Java program?
class Test {
int i;
}
class Main {
public static void main(String args[]) {
Test t;
System.out.println(t.i);
}
0
garbage value
Compiler Error
Run Time Error
Predict the output of following Java program
class Test
{ int i;
}
class Main
{ public static void main(String args[])
{ Test t = new Test();
System.out.println(t.i);
} }
Garbage Value
0
Compiler Error
Runtime error
A way to discover which of the two classes is the base class and which is the subclass is to _______.
look at the class size
try saying the two class names together
use polymorphism
Both a and b are correct
Employing inheritance reduces errors because ______.
the new classes have access to fewer data fields
the new classes have access to fewer methods
you can copy methods that you already created
many of the methods you need have already been used and tested
A base class can also be called a ______.
child class
subclass
derived class
superclass
The Java keyword that creates inheritance is _____.
static
enlarge
extends
inherits
A class named Building has a public, nonstatic method named getFloors(). If School is a child class of Building, and modelHigh is an object of type School, which of the following statements is valid?
Building.getFloors();
School.getFloors();
modelHigh.getFloors();
All of the previous statements are valid.
Which of the following statements is true?
A child class inherits from a parent class.
A parent class inherits from a child class.
Both of the preceding statements are true.
Neither a nor b is true.
How would you declare and initialize a button in Java?
(use button as the object name and with caption "Click me!") (5 points)
(a)
In Java, everything is an Object. Java can be easily extended since it is based on the Object model. Which characteristic of Java was described?
Object-oriented
Simple
Secure
Robust
Which feature makes it possible to write programs that can perform many tasks simultaneously.
Interpreted
Multithreaded
Efficient
Multi-purpose
On what year was Oak renamed as Java
1998
1999
1996
1995
What is the name of the company that developed Java?
Sun Microsystems
IBM
The Green Team
Microsoft
What is the file extension for Java Source Code?
.java
.class
.exe
.obj
What is the old name for Java Programming?
Java2
Oak
C++
Greenteam
Tools that provide support for software development process including editors and debuggers are called
integrated development environments
bytecode converters
virtual machine converters
memory verifier
Which of the following is not a feature of Java
Robust
Platform Specific
Multithreaded
Object-Oriented
From where did Java get its name?
Coffee
Book
Initials of the developers of Java
TV programme
Who invented Java?
Dennis Ritchie
Ken Thompson
Bjarne Stroustrup
James Gosling
This java version is meant to have large number of users
JAVA EE
JAVA SE
JAVA ME
Java is familiar because
Memory allocation and Garbage Collection is taken care of
Patterned from C++
Components of the program are treated like objects
A code written using any programming language that is translated into binary code is called
Source code
Bytecode
Pseudocode
Flowchart
What is the file extension for Java Executable Code?
.java
.class
.exe
.obj
It is a type of java program that can be embedded in a web.
general purpose application
standalone applications
applet
webpage
Java programs ________________
are only compiled
are only compiled not interpreted
are only interpreted
are both compiled and interpreted
What are the values of var1 and var2 when the code finishes executing?
var1 = 1, var2 = 1
var1 = 2 , var2 = 0
var1 = 3, var2 = -1
var1 = 0, var2 = 2
The loop will cause a run-time error for division by zero
What are the values of x and y when the code finishes executing?
x = 5, y = 2
x = 2, y = 5
x = 3, y = 4
x = 4, y = 3
Infinite Loop
How many stars are printed?
7
3
4
5
Infinite Loop
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
How many times does the loop print a *
40
20
24
30
Infinite Loop
What does the following code print?
A rectangle of 8 rows with 5 stars per row
A rectangle of 8 rows with 4 stars per row
A rectangle of 6 rows with 5 stars per row
A rectangle of 6 rows with 4 stars per row
Infinite Looo
What does the following code print?
A rectangle of 9 rows with 5 stars per row
A rectangle of 6 rows with 6 stars per row
A rectangle of 7 rows with 5 stars per row
A rectangle of 7 rows with 6 stars per row
Infinite Loop
How many times is "HI!" printed?
15
12
10
8
Infinite Loop
What does the following code print?
5 6 7 8 9
4 5 6 7 8 9 10 11 12
3 5 7 9 11
3 4 5 6 7 8 9 10 11 12
How many times does the following method print a *?
9
6
7
10
What does the following code segment print?
5 4 3 2 1
-5 -4 -3 -2 -1
-4 -3 -2 -1 0
How many times does the code print a * ?
7
8
12
13
How many times does this print a * ?
4
5
6
7
How many times does this code print at * ?
5
6
7
8
What does the code print?
1 2 3 4
1 2 3 4 5
0 1 2 3 4
0 1 2 3 4 5
How many stars are printed?
10
5
25
50
What is printed when the code segment is executed?
0 2 4 6 8 10 12 14 16 18
0 6 12 18
1 4 7 10 13 16 19
4 10 16
What is polymorphism in Java?
Polymorphism in Java is only applicable to variables
Polymorphism in Java is the ability of a single method to perform different actions based on the object it is acting upon.
Polymorphism in Java is a feature that allows static binding
Polymorphism in Java is the ability to use multiple inheritance
Explain the concept of method overriding in polymorphism.
Method overriding is the ability of a subclass to provide a specific implementation of a method that is already provided by its parent class.
Method overriding is the ability of a subclass to provide a completely new method that is not related to the parent class method.
Method overriding is the ability of a subclass to inherit the method implementation from the parent class without any changes.
Method overriding is the ability of a subclass to provide a general implementation of a method that is already provided by its parent class.
How does polymorphism help in achieving dynamic method dispatch?
Polymorphism enables superclass references to subclass objects, allowing for dynamic method dispatch.
Polymorphism causes errors when trying to call methods from subclass objects.
Polymorphism has no impact on method dispatch, it only affects variable binding.
Polymorphism restricts superclass references to subclass objects, hindering dynamic method dispatch.
What is the difference between compile-time polymorphism and runtime polymorphism?
Compile-time polymorphism is resolved during program execution
The difference lies in how the polymorphism is resolved - compile-time polymorphism is resolved during compilation, while runtime polymorphism is resolved during program execution.
Runtime polymorphism is resolved during compilation
Both compile-time and runtime polymorphism are resolved during compilation
Give an example of polymorphism using method overloading.
Using method overloading with only one method
Implementing method overloading with different return types
Having multiple 'calculate' methods with different parameter types such as int, double, and String.
Having multiple 'calculate' methods with the same parameter type
Discuss the importance of polymorphism in object-oriented programming.
Polymorphism limits the functionality of OOP
Polymorphism leads to code duplication in OOP
Polymorphism is irrelevant in OOP
Polymorphism is important in OOP for flexibility, reusability, and extensibility.
Explain the use of the 'instanceof' operator in polymorphism.
The 'instanceof' operator is used to check if an object is null in polymorphism.
The 'instanceof' operator is used to determine the size of an object in polymorphism.
The 'instanceof' operator is used to perform mathematical operations on objects in polymorphism.
The 'instanceof' operator is used to determine if an object is an instance of a particular class or interface in polymorphism.
What are the benefits of using polymorphism in Java?
Polymorphism leads to slower performance in Java programs
Polymorphism only works with primitive data types
Polymorphism helps achieve flexibility and code reusability by allowing objects of different classes to be treated as objects of a common superclass.
Polymorphism makes code harder to read and understand
Provide a real-world example where polymorphism can be applied effectively.
Software application managing different shapes
Hardware device managing different sizes
Software application managing different colors
Online platform managing different animals
What does the word “poly” in polymorphism mean?
many
one
two
three
Java supports:
run time polymorphism
compile time polymorphism
both run time and compile time polymorphism
neither run time nor compile time polymorphism
What does 'Simple' mean in the context of Java features?
Complexity and difficulty in language design
Simplicity and ease of use in language design
Elaborate and intricate coding structures
Advanced and convoluted programming concepts
Why is Java considered 'Secured'?
Java is not considered 'Secured' because it has many vulnerabilities.
Java is considered 'Secured' due to its slow performance.
Java has bytecode verification, classloader, and security manager features.
Java is not considered 'Secured' because it lacks encryption capabilities.
Explain the concept of 'Distributed' in Java.
Java 'Distributed' concept enables programs to run on multiple networked computers.
Java 'Distributed' concept allows programs to run on a single computer only.
Java 'Distributed' concept is not related to networking.
Java 'Distributed' concept is only applicable to mobile applications.
