Java Foundations Final Exam | PDF | Programming | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
53 views

Java Foundations Final Exam

The document contains questions about Java programming concepts like loops, classes, objects, arrays and exceptions. It also includes code snippets and asks for the expected output. Some key points: - A break statement terminates the current loop and continues execution after the loop. - A continue statement skips the current loop iteration and continues with the next. - The for loop header contains initialization, condition and increment expressions that are executed each iteration. - Classes can contain multiple constructors to initialize objects. Fields are initialized using this.field = value. - Arrays provide indexed access to a collection of values of the same type.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Java Foundations Final Exam

The document contains questions about Java programming concepts like loops, classes, objects, arrays and exceptions. It also includes code snippets and asks for the expected output. Some key points: - A break statement terminates the current loop and continues execution after the loop. - A continue statement skips the current loop iteration and continues with the next. - The for loop header contains initialization, condition and increment expressions that are executed each iteration. - Classes can contain multiple constructors to initialize objects. Fields are initialized using this.field = value. - Arrays provide indexed access to a collection of values of the same type.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. Which two statements are true about the break statement?

(Choose all correct answers)


____________________
When a break statement is executed inside a loop, the loop-statement is terminated
immediately.
The execution of the program will continue with the statement following the loop-
statement.

2. What is the result?

public static void main(String[] args) {


for (int var1 = 0; var1 < 2; var1++) {
for (int var2 = 0; var2 < 2; var2++) {
if (var2 == 2) {
continue;
}
System.out.println("var1:" + var1 + ", var2:" + var2);
}
}
}
________________
var1: 0, var2: 0
var1: 0, var2: 1
var1: 1, var2: 0
var1: 1, var2: 1

3. The while loop continually executes a block of statements while a particular


condition is false.
___________________
false

4.Which of the two are pre-test loops?


(Choose all correct answers)
________________
while
for

7. Each expression in the header section of a for loop is optional.


________________
true

8. Which is not a looping statement in Java?


________________
switch

11. Which two statements are true about getter methods?


____________________
Getters usually accept no arguments.
Getters have a public access modifier.

12. Access and visibility of a class should be limited as much as possible.


_____________________
true

13. A constructor is a special method which is commonly used to set the initial
values of an object’s fields.
___________________
true

14. How could you write the Employee constructor so that its parameters are named
the same as the fields they’re initializing?

public class Employee{


private String name;
private double salary;
public Employee(String name, double salary){
//initialize name
//initialize salary
}
}
__________________
public Employee(String name, double salary){
this.name = name;
this.salary = salary;
}

15. Which has a default value of null?


_________________
string

16. Which statement is true about the default constructor of a class?


________________________
Java automatically provides a constructor for every class.

19. Class name should follow Camel casing rules.


___________________
true

20. How can you retrieve a value from a method?


________________
Use a return statement and define the method’s return type as non-void

21. In the following statements, how many employee objects are created?

Employee e1 = new Employee();


Employee e2 = new Employee();
Employee e3 = new Employee();

_________________
3

24. An object must be instantiated before its non-static fields and methods can be
accessed.
_______________________
true

26. Methods can call other methods in the same class.


___________________
true

28. You can write more than one constructor in a class.


_______________
true

30. Each catch block is an exception handler that handles the type of exception
indicated by its argument.
___________________
true

31. Runtime errors can be caught by Java’s exception handling mechanism.


____________________
true

32. Identify where there is a potential bug in this code:

int radiusOfCircle = 10;


int areaOfCircle = Math.PI * radiusOfCircle * radiusOfCircle;
_______________________
A datatype is incorrect.

33. Which is not a compilation error?


_____________________
int y;
y++;

34. Testing and debugging are important activities in software development.


______________________
true

36. Which is not used to traverse an ArrayList?


_________________
do- while loop

37. You can access elements in an ArrayList by their index.


___________________
true

38. Which of the following is not a wrapper class?


______________________
String (*)

39. What is the starting index of an array?


_____________________
0

40. What is the output?


int[] arr = new int[1];
arr[0] = 10;
System.out.println(arr[0]);
__________________
10

41. What is an array?


________________
An array is an indexed container that holds a set of values of a single type. (*)

43. Audio can be played by referencing the AudioClip object directly.


____________________
true

44. Which method is used to for mouse click events?


__________________
setOnMouseClicked()

48. How would you set the title of the Stage primaryStage?
_____________________
primaryStage.setTitle("New Title!");

49. A layout Pane dictates how Nodes must be positioned


________________
true

You might also like