Questions and Answers
The try{}
block must always be used to capture an exception.
True
The StringTokenizer class is used for:
Consider the following code fragment: public class ExceptionHandlingNote { public static void main(String[] args) {String x; System.out.println(x);}}
What will it print?
Tokenizing the string 'a,b;c,d;e,f' using the comma delimiter results in:
Signup and view all the answers
Consider the following array myArray with elements {7,9,-3,6,1,-1}. What is the value of myArray[myArray - myArray]?
Signup and view all the answers
You can use the Java method __ from the Color class to change the drawing color.
Signup and view all the answers
Any change to graphics being displayed on the screen requires a call to the method __ to update the graphics.
Signup and view all the answers
The ActionListener interface requires that the method __ be implemented.
Signup and view all the answers
The correct syntax for accessing the length of an array named numbers is:
Signup and view all the answers
To use the Java class ArrayList, you must import the package:
Signup and view all the answers
An ArrayIndexOutOfBounds error is:
Signup and view all the answers
To place an element in an ArrayList position for the first time, you usually use the method:
Signup and view all the answers
A button fires events known as:
Signup and view all the answers
A button control should have a registered _____ associated with it to take some action when the user clicks on it.
Signup and view all the answers
Consider the following array declaration inside the body of a method. int[] values = new int;
Accessing the array element, values, right after the above declaration results in:
Signup and view all the answers
Suppose an ArrayList of String objects, myList, has the strings '1', '2', '3' added to it in that order. The code ListIterator it = myList.listIterator(); while (it.hasNext()) {System.out.print(it.next()); it.previous();}
results in the following output:
Signup and view all the answers
In event-driven programming, a Java program includes one or several of the following steps: 1. Create a component object and add it to the container 2. Specify which class implements a component's event listener 3. Implement an event handler method and specify what program method calls it 4. Register the component as a listener for the application frame. Which steps actually have to be included?
Signup and view all the answers
Consider the following ArrayList declaration inside the body of a method. ArrayList myList = new ArrayList();
Accessing the element, myList.get()
, right after the above declaration results in:
Signup and view all the answers
The __ is an object that waits for an event to occur and responds in some way when it does.
Signup and view all the answers
Which of the following initializes the elements of an array named myDoubles?
Signup and view all the answers
Consider the following ArrayList declaration inside the body of a method. ArrayList myList = new ArrayList();
Which of the following statements are true before the above ArrayList is modified? More than one choice may be applicable.
Signup and view all the answers
Consider the following array declaration inside the body of a method. int[][] values = {{1}, {2,3}, {4,5,6}};
The expression (values + values)
results in:
Signup and view all the answers
Consider the following array declaration inside the body of a method. int[][] values = {{1},{2,3},{4,5,6}};
The expression (values + values)
results in:
Signup and view all the answers
Given the sequence of the following statements, g.drawLine(50,100,150,100); g.drawLine(50,50,150,50); g.drawLine(150,50,150,100); g.drawLine(50,50,50,100);
which of the following can replace the above sequence?
Signup and view all the answers
Suppose an ArrayList of String objects, myList, has the strings '1', '2', '3' added to it in that order. The code Iterator it = myList.iterator(); while (it.hasNext()) {System.out.print(it.next());}
results in the following output:
Signup and view all the answers
Which of the following draws a circle whose center position is (400,400) and whose radius is 50?
Signup and view all the answers
Consider the following array declaration inside the body of a method. String [] values = new String;
Invoking the length method on the array element, values, right after the above declaration results in:
Signup and view all the answers
Consider the following ArrayList declaration and its iterator inside the body of a method. ArrayList myList = new ArrayList(); Iterator it = myList.iterator();
Invoking the it.next()
method right after the above declaration results in:
Signup and view all the answers
Suppose we have an array of String objects (with at least one element) identified by the variable 'names'. Which of the following loops will CORRECTLY process each element in the array?
Signup and view all the answers
Which of the following is correct? (Answer verified)
Signup and view all the answers
Which of the following statements would compile and execute without errors? (Answer verified)
Signup and view all the answers
If your class does not have the toString() method, which of the following will happen when the object is printed to the console?
Signup and view all the answers
After executing the following piece of code, what are the colors of r1 and r2 (in that order)?
Signup and view all the answers
What happens if MyClass.printSomething() is called from another class?
Signup and view all the answers
Which of the following is NOT correct about accessor and mutator methods?
Signup and view all the answers
Which of the following is an incorrect statement about imperative (procedural) programming and object-oriented programming?
Signup and view all the answers
What method signature would be best for isSpeeding?
Signup and view all the answers
Which of the following statements is false?
Signup and view all the answers
The ________ of an object define its potential behaviors.
Signup and view all the answers
Which of the following does not overload the method: public int myMethod(double a, int i)
Signup and view all the answers
Analyze the following code. What is the output?
Signup and view all the answers
A variable whose meaning is confined to a method definition is called an/a
Signup and view all the answers
A variable whose meaning is confined to an object of a class is called
Signup and view all the answers
The modifier private means that an instance variable can be accessed by name outside of the class definition.
Signup and view all the answers
Which of the following statements is true about the static modifier?
Signup and view all the answers
Which of the following is NOT correct about declaring a class?
Signup and view all the answers
Consider the following code: What is the output when executed?
Signup and view all the answers
Which of the following statements is NOT correct about constructors?
Signup and view all the answers
A method that performs some actions but does not return a value can be declared a _____ method.
Signup and view all the answers
When you want the parameters in a method to be the same as the instance variables, you use the ____ keyword.
Signup and view all the answers
What is the output of running the class C?
Signup and view all the answers
What is the output generated when Test is run considering the following code?
Signup and view all the answers
Which of the following code fragments would generate a compile error?
Signup and view all the answers
Analyze the following code. What is the output?
Signup and view all the answers
What is the difference between static variable and instance variable?
Signup and view all the answers
A class can be defined by extending another class. It is called ____.
Signup and view all the answers
Among the given choices, which block immediately follows a try block?
Signup and view all the answers
A ____ block executes regardless of whether an exception occurs or not.
Signup and view all the answers
Given String str = 'Java Programming!'; the following statement returns false. str == 'Java Programming!';
Signup and view all the answers
In Java, when you open a text file you should account for a possible
Signup and view all the answers
The execution of a throw statement is referred to as _____
Signup and view all the answers
A runtime exception is a _____
Signup and view all the answers
Exceptions that are subject to the catch or declare rules are called ______
Signup and view all the answers
The stream that is automatically available to your Java code is _____
Signup and view all the answers
Given String str = 'Java Programming!'; the statement System.out.println(str.substring(5,8)); produces the output _____
Signup and view all the answers
The class ObjectOutputStream contains all of the following methods except:
Signup and view all the answers
Which operator is used to concatenate two strings?
Signup and view all the answers
If a method does not catch an exception, then it must at least warn programmers that any invocation of the method might possibly throw an exception. This warning is done using _____
Signup and view all the answers
When an exception is thrown, the code in the surrounding try block continues executing and then the catch block begins execution.
Signup and view all the answers
The output stream connected to the computer screen is
Signup and view all the answers
Try blocks contain code that could possibly
Signup and view all the answers
When the method readLine() tries to read beyond the end of a file, it returns the value of _____
Signup and view all the answers
Which one of the following classes from the java.io package can be used for writing text data to a file?
Signup and view all the answers
The method trim() of the String class trims of
Signup and view all the answers
Tokenizing the string 'a,b;c,d;e,f' using the semi-colon delimiter results in
Signup and view all the answers
The code new StringBuffer('abcd').reverse().substring(1,2) results in the following string;
Signup and view all the answers
The catch clause has _____ parameter(s)
Signup and view all the answers
If an object must be written to and read back from a file in its entirety, the class definition of that object must
Signup and view all the answers
Which of the following statements is true about exception handling?
Signup and view all the answers
Which one of the following classes from the java.io package can be used for writing objects to a file?
Signup and view all the answers
Suppose String s = 'Java'; which of the following will change s to 'java'. More than one choice is valid.
Signup and view all the answers
What is the value of variable s after the following program: String s1 = new String('Make the world better as always'); char s = s1.charAt(length()-2);
Signup and view all the answers
Which of the following are the CORRECT ways of declaring a string?
Signup and view all the answers
Study Notes
Java Class Quiz Set Overview
- A collection of flashcards covering Java programming concepts through multiple-choice questions.
- Each flashcard presents a question with possible answers to assess understanding of Java classes and features.
Key Java Concepts from Flashcards
-
CS520 Class Example
- Correct answer: Compiles and prints out 20.
- Demonstrates instantiation and access of a private instance variable.
-
Employee Class
- Correct statement: Instance of Employee can access and modify its public variables and methods.
- Indicates proper usage of constructors in Java.
-
toString() Method
- If not overridden, printing an object will run fine; defaults to Object's toString().
-
Object References
- In a shared reference scenario, modifying one object impacts the other (e.g., r1 and r2).
-
Variable Scope
- Instance variable (i) cannot be accessed within static context without creating an instance.
-
Accessor and Mutator Methods
- Customary naming conventions: Accessors with "get" prefix, mutators with "set".
-
Programming Paradigms
- Object-oriented programming uses objects to represent real-world entities.
- Imperative programming emphasizes function calls and procedure sequences.
Instance Methods and Variables
- Methods can return types and modify instance variables. Example given for isSpeeding in a Car class.
- Instance variables maintain state; local variables are confined to specific method execution.
Error Handling and Exceptions
- Exceptions must be accounted for, especially when handling files or user inputs.
- Runtime exceptions are unchecked and do not need explicit handling.
- Checked exceptions must be either caught or declared in the method signature.
String Handling
- Strings are immutable; operations create new instances.
- Common string methods:
toLowerCase()
,replace()
,trim()
are significant for manipulation.
Array and Color Handling
- Use of array indexing to manipulate data within arrays.
- Graphics color rendering requires methods to update the display properly.
ActionListener Interface
- Essential for event handling in GUI; requires implementing the method
actionPerformed()
.
Summary
- Understanding of class structures, variable scopes, methods, error handling, and string manipulations is crucial in Java.
- Practicing these concepts through multiple-choice questions reinforces learning and application of Java principles.### Array Access and Basics
- Access the 5th element in an array by using
colors[4]
, as array indexing starts from 0. - An ArrayIndexOutOfBounds error is a logic error that occurs when attempting to access an invalid index in an array.
- To place an element in an ArrayList at a new position, use the
add()
method.
Utilizing Java Libraries
- To use the Java class ArrayList, import the package
java.util
.
Event Handling in Java
- In event-driven programming, a button must have a registered listener to handle user clicks, known as an event.
- A button fires action events when interacted with by the user.
ArrayList Characteristics
- Upon declaration, an ArrayList's initial size is 0, and its capacity is usually 10 in Java.
- Attempting to access an element in an empty ArrayList with
myList.get()
results in a runtime exception. - Initialize elements of an array in Java using the syntax:
double[] myDoubles = {0.0, 1.0, 1.5, 2.0, 2.5};
.
Multi-dimensional Arrays
- Accessing elements in a two-dimensional array, such as
int[][] values = {{1}, {2,3}, {4,5,6}}
, involves using row and column indices. - The expression
values + values
works based on the computed sum of elements, which is 9 in this case (sum of 3 and 6).
Looping and Iteration
- Use a
for
loop with the correct syntax to iterate through array elements:for (int i = 0; i < names.length; i++) {...}
. - An iterator for an ArrayList starts at an initial position without any elements, and calling
it.next()
would cause a runtime exception if no elements are present.
Graphics in Java
- To draw a circle, utilize
g.drawOval(x, y, width, height)
since there is no direct method fordrawCircle
. - Understand dimensions in graphics functions, as the radius is related to width and height for oval shapes.
Common Errors and Exceptions
- An uninitialized array or ArrayList can lead to exceptions when methods like
length
orget()
are called. - Always ensure initial values are assigned to arrays to avoid compilation errors.
Summary of Key Concepts
- Recognize the distinction between arrays and ArrayLists in Java, especially regarding their declarations and methods.
- Event listeners are crucial for interactive components in Java applications.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of Java class structures with this quiz focusing on accessibility and compilation. Answer questions based on class definitions and variable visibility. Perfect for beginners in Java programming!