Skip to main content

Java Programming Practice Questions - 639 Verified Questions

Page 1


Java Programming Practice Questions

Course Introduction

Java Programming is an introductory course designed to equip students with the foundational skills and knowledge necessary to develop robust software applications using the Java programming language. The course covers key programming concepts, including variables, data types, control structures, object-oriented principles, exception handling, and basic data structures. Through hands-on exercises and projects, students gain practical experience in designing, coding, testing, and debugging Java programs, while also learning industry-standard development tools and methodologies. By the end of the course, students will be able to create functional Java applications and have a solid grounding for advanced study in computer science and software engineering.

Recommended Textbook

Java Foundations 3rd Edition by John Lewis

Available Study Resources on Quizplus

16 Chapters

639 Verified Questions

639 Flashcards

Source URL: https://quizplus.com/study-set/3997 Page 2

Chapter 1: Introduction

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80262

Sample Questions

Q1) Syntax rules dictate the form of a program. Semantics dictate the meaning of the program statements.

A)True

B)False

Answer: True

Q2) Which of the following might be included in an IDE?

A) a compiler

B) an editor

C) a debugger

D) all of the above

E) none of the above

Answer: D

Q3) Java is _____________________.

A) a procedural language

B) a functional language

C) an object-oriented language

D) a fourth-generation language

E) a spoken-language

Answer: C

To view all questions and flashcards with answers, click on the resource link above.

Page 3

Chapter 2: Data and Expressions

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80257

Sample Questions

Q1) Write a short program that declares a single integer variable called age and assigns it a value equal to your age. Also have it print out your age using the age variable. The output of your program should look similar to the following: I am 30 years old.

Answer: 11edd9cf_f07e_e94c_84e8_d51ecd69a191_TB2841_00

Q2) Explain the difference between the print and the println methods.

Answer: The println method performs a carriage return after printing the specified characters. This means the next output will begin on the next line. The print method does not do this, and therefore the next output will begin right after the previous output.

Q3) Which of the following is not an arithmetic operation in Java?

A) +

B) -

C) *

D) %

E) These are all arithmetic operations in Java

Answer: E

To view all questions and flashcards with answers, click on the resource link above.

Chapter 3: Using Classes and Objects

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80255

Sample Questions

Q1) The new operator is used to access an objects methods.

A)True

B)False

Answer: False

Q2) Suppose a Random object has been created called generator. Write a single statement that generates a random number in the range 73 to 100.

Answer: randNum = generator.nextInt(28) + 73;

Q3) Which of the following is an invalid way to instantiate a String object?

A) String title = new String("Java Software Solutions");

B) String name = "John Lewis";

C) String empty = "";

D) String alsoEmpty = new String("");

E) all of the above are valid

Answer: E

Q4) What is the range of integers that will be generated by the following expression? generator.nextInt(15) + 5;

Answer: This expression will generate a random number in the range 5 to 19 (inclusive).

To view all questions and flashcards with answers, click on the resource link above.

5

Chapter 4: Conditionals and Loops

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80254

Sample Questions

Q1) What is output by the following code fragment? int num = 0; int max = 10; while(num < max) { System.out.println(num); num += 2; }

Q2) In a nested if statement an else clause is matched to the closest unmatched if. A)True B)False

Q3) Every if statement requires an associated else statement, but not every else statement requires an associated if statement. A)True B)False

Q4) Using a while loop, write a code fragment that computes the sum of the first n natural numbers (i.e. 1, 2, 3, 4, ... n), where n is an integer variable already defined in the program.

Q5) Write a switch statement that switches on an integer variable named val. If val is 2 or 15, then output "Hello World." For all other values, output "Goodbye World."

To view all questions and flashcards with answers, click on the resource link above. Page 6

Chapter 5: Writing Classes

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80253

Sample Questions

Q1) A(n) ___________________ object is one that is made up, at least in part, of other objects.

A) static

B) aggregate

C) encapsulated

D) private

E) public

Q2) A return statement is not required at the end of every method.

A)True

B)False

Q3) The ________________ reference always refers to the currently executing object.

A) null

B) static

C) final

D) actual

E) this

Q4) Write a method called randomAverage that generates 100 random integers in the range 1 to 100 (inclusive) and returns their average. You may assume that the class has a static Random object called generator already declared and instantiated.

To view all questions and flashcards with answers, click on the resource link above. Page 7

Chapter 6: Graphical User Interfaces

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80252

Sample Questions

Q1) Explain the difference between check boxes and radio buttons.

Q2) Which of the following components can be toggled on or off using the mouse, indicating that a particular boolean condition is set or unset?

A) check box

B) radio button

C) slider

D) dialog box

E) none of the above

Q3) Check boxes operate as a group, providing a set of mutually exclusive options.

A)True

B)False

Q4) Suppose we have created a class called MyGUI, which represents a GUI. Write a program that creates a JFrame object, adds a MyGUI object to the frame and makes it visible.

Q5) Describe the difference between a heavyweight container and a lightweight container. Give an example of each.

Q6) Write a short class that represents a panel with a single radio button that has the option "Yes" and the option "No." By default, the Yes button should be checked.

To view all questions and flashcards with answers, click on the resource link above. Page 8

Chapter 7: Arrays

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80251

Sample Questions

Q1) In Java, array indexes begin at 0 and end at one less than the length of the array.

A)True

B)False

Q2) What will be the output of the following code snippet? int[] array = new int[25]; System.out.println(array.length);

A) 26

B) 24

C) 25

D) This code will result in a compile time error.

E) This code will result in a run time error.

Q3) Which of the following lines of code accesses the second element of the first array in a two-dimensional array of integers, numbers, and stores the result in a variable called num?

A) num = numbers[1][2];

B) num = numbers[0][1];

C) num = numbers.getElement(1, 2);

D) num = numbers.getElement(0, 1);

E) none of the above are correct

To view all questions and flashcards with answers, click on the resource link above.

9

Chapter 8: Inheritance

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80250

Sample Questions

Q1) Suppose we create a subclass from a class that has a method called someMethod. If we override someMethod in the subclass, is it possible to access the superclass's version of someMethod? If so, how?

Q2) The process of inheritance should establish a(n) ___________________ relationship.

A) is-a B) has-a C) static D) not-a

E) none of the above

Q3) Explain the relevance of the Object class to the Java programming language.

Q4) What does it mean for a class to be declared as final? What does it mean for a method to be declared as final?

Q5) Once a method is overridden in a subclass, the original version can never be referenced from within the subclass.

A)True

B)False

Q6) Describe the behavior of the toString method and the equals method of the Object class.

To view all questions and flashcards with answers, click on the resource link above. Page 10

Chapter 9: Polymorphism

Available Study Resources on Quizplus for this Chatper

39 Verified Questions

39 Flashcards

Source URL: https://quizplus.com/quiz/80249

Sample Questions

Q1) What is an interface?

Q2) Which of the following methods are included with any object that implements the Iterator interface?

A) next

B) hasNext

C) toString

D) all of the above

E) a and b

Q3) A parameter to a method can be polymorphic.

A)True

B)False

Q4) The commitment to execute certain code to carry out a method invocation is referred to as _________________.

A) execution

B) binding

C) polymorphism

D) inheritance

E) none of the above

Q5) What is polymorphism?

Q6) Can references to interface types be polymorphic? Explain.

Page 11

To view all questions and flashcards with answers, click on the resource link above.

Chapter 11: Recursion

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80260

Sample Questions

Q1) Recursion is necessary.

A)True

B)False

Q2) In Java, it is possible for a method to call itself.

A)True

B)False

Q3) Give a recursive definition of the sum of the first n integers, S(n).

Q4) A method that calls a different method, which then calls the original calling method is a recursive method.

A)True

B)False

Q5) ____________________ recursion occurs when a method calls itself, while _________________ recursion when a method calls another method that then calls the original method.

A) upward, downward

B) downward, upward

C) direct, indirect

D) indirect, direct

E) none of the above

Q6) Write the recursive definition of the factorial of a number.

Page 12

To view all questions and flashcards with answers, click on the resource link above.

Chapter 10: Exceptions

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80261

Sample Questions

Q1) What is the difference between a checked exception and an unchecked exception?

Q2) What is exception prorogation, and how does it work in Java?

Q3) Which of the following methods are part of the Exception class and can be used to give information about a thrown exception?

A) getInfo

B) printInfo

C) printStackTrace

D) getStackTrace

E) none of the above

Q4) The Exception class and the Error class are subclasses of the ___________________ class.

A) Throwable

B) Catchable

C) RuntimeProblem

D) CompilerProblem

E) none of the above

Q5) Is the Exception class a subclass of the Error class? Explain.

Q6) How does a method throw an exception?

Q7) What is the difference between an exception and an error?

Q8) Is an exception an object? Explain.

To view all questions and flashcards with answers, click on the resource link above. Page 13

Chapter 12: Searching and Sorting

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80259

Sample Questions

Q1) As the number of items in a search pool grows, the number of comparisons required to search _______________ .

A) increases

B) decreases

C) stays the same

D) goes to 0

E) none of the above

Q2) ____________________ is the process of arranging a group of items into a defined order.

A) Searching

B) Sorting

C) Selecting

D) Helping

E) none of the above

Q3) Write a method that accepts an integer and an array of integers, and returns true if the integer is contained in the array. You may assume that the array is sorted, and your method should use a binary search..

Q4) Write a method that accepts an integer array as a parameter and sorts it using the bubble sort algorithm.

To view all questions and flashcards with answers, click on the resource link above. Page 14

Chapter 13: Trees

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80258

Sample Questions

Q1) One method of implementing a tree using an array involves storing the child of the element at the index n in the array at indexes

A) n+1 and n+2

B) 2<sup>n</sup> and 2<sup>2n</sup>

C) 2n+1 and 2n+2

D) n-1 and n-2

E) none of the above will work

Q2) A binary tree is a tree in which any node can have at most two children.

A)True

B)False

Q3) Which of the following traversals is not easily implemented recursively?

A) Preorder

B) Inorder

C) Postorder

D) Level-order

E) all of the above are easily implemented recursively

Q4) What might be the difference between a complete binary tree and a full binary tree?

Q5) Explain the implementation of an postorder traversal.

To view all questions and flashcards with answers, click on the resource link above. Page 15

Chapter 14: Introduction to Collections and Stacks

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80248

Sample Questions

Q1) What is the fundamental difference between radix sort and the other sorting techniques that have been studies.

Q2) Suppose there were no count variable stored for the linked implementation of a queue. Would it still be possible to implement a constant time size operation? How about if there were no count variable stored for an array based implementation of a queue?

Q3) A stack is the ideal collection to use when _______________________ .

A) implementing a radix sort

B) evaluating a postfix expression

C) evaluating an infix expression

D) implementing a quick sort

E) none of the above

Q4) Write an enqueue method for a queue implemented as a circular array. You may assume that you have access to a method called expandCapacity that will double the size of the array if necessary. The class has instance variables front and rear, which represent the indexes of the front and rear of the queue. It also has an integer variable called count that represents the number of elements in the queue, as well as an array of generic T types called queue that represents the queue.

Q5) List the five basic operations on a stack.

To view all questions and flashcards with answers, click on the resource link above. Page 16

Chapter 15: Heaps and Priority Queues

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80247

Sample Questions

Q1) In a balanced binary search tree, adding an element always requires approximately O(log<sub>2</sub> n) steps.

A)True B)False

Q2) A heap sort sorts elements by constructing a heap out of them, and then removing them one at a time from the root.

A)True B)False

Q3) In a binary search tree, a new element is always added as a leaf. A)True B)False

Q4) The most efficient binary search trees are balanced. A)True B)False

Q5) Suppose we have a class called BinaryTree that includes a find method. If we extend the class to create a BinarySearchTree class, should we override the find method or use it as is? Explain.

Q6) Describe how to find an element in a binary search tree. You may use English sentences or pseudocode.

17

To view all questions and flashcards with answers, click on the resource link above.

Chapter 16: Graphs

Available Study Resources on Quizplus for this Chatper

40 Verified Questions

40 Flashcards

Source URL: https://quizplus.com/quiz/80256

Sample Questions

Q1) A spanning tree of a graph is a tree with that always has which of the following properties?

A) It includes some of the edges and some of the vertices of the graph.

B) It includes all of the edges and some of the vertices of the graph.

C) It includes some of the edges and all of the vertices of the graph.

D) It includes all of the edges and all of the vertices of the graph.

E) none of the above

Q2) In a(n) _____________________ graph, an edge from node labeled A to a node labeled B is the same as having an edge from B to A.

A) directed

B) undirected

C) sparse

D) tree-like

E) none of the above

Q3) Explain how a breadth-first traversal of a graph works.

Q4) Write out all of the edges in a complete graph on 4 vertices.

Q5) A digraph and an directed graph are the same thing.

A)True

B)False

Q6) What does it mean for a graph to be connected?

Page 18

To view all questions and flashcards with answers, click on the resource link above.

Turn static files into dynamic content formats.

Create a flipbook