Java Class Basics Quiz
83 Questions
100 Views

Java Class Basics Quiz

Created by
@FoolproofLemur

Questions and Answers

The try{} block must always be used to capture an exception.

True

The StringTokenizer class is used for:

  • Converting the given string from one format to another format
  • Finding the positions of the delimiters in the given string
  • Examining the individual tokens in the given string based on the specified delimiters (correct)
  • Examining each character in the given string
  • Consider the following code fragment: public class ExceptionHandlingNote { public static void main(String[] args) {String x; System.out.println(x);}} What will it print?

  • It prints out nothing
  • It prints out 0
  • None of the choices is true
  • It prints out null (correct)
  • Tokenizing the string 'a,b;c,d;e,f' using the comma delimiter results in:

    <p>4 tokens</p> 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]?

    <p>-3</p> Signup and view all the answers

    You can use the Java method __ from the Color class to change the drawing color.

    <p>setColor</p> 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.

    <p>repaint</p> Signup and view all the answers

    The ActionListener interface requires that the method __ be implemented.

    <p>actionPerformed()</p> Signup and view all the answers

    The correct syntax for accessing the length of an array named numbers is:

    <p>numbers.length</p> Signup and view all the answers

    To use the Java class ArrayList, you must import the package:

    <p>java.util</p> Signup and view all the answers

    An ArrayIndexOutOfBounds error is:

    <p>logic error</p> Signup and view all the answers

    To place an element in an ArrayList position for the first time, you usually use the method:

    <p>add()</p> Signup and view all the answers

    A button fires events known as:

    <p>action events</p> 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.

    <p>listener</p> 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:

    <p>A compilation error since there is initial value assigned to it</p> 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:

    <p>An infinite number of 1's</p> 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?

    <p>1 and 2</p> 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:

    <p>An exception that will be thrown at runtime</p> 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.

    <p>Listener</p> Signup and view all the answers

    Which of the following initializes the elements of an array named myDoubles?

    <p>double [] myDoubles = {0.0, 1.0, 1.5, 2.0, 2.5};</p> 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.

    <p>The size of the above ArrayList is 0</p> 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:

    <p>9</p> 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:

    <p>A syntax or compilation error</p> 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?

    <p>g.drawLine(50,100,100,50);</p> 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:

    <p>123</p> Signup and view all the answers

    Which of the following draws a circle whose center position is (400,400) and whose radius is 50?

    <p>g.drawOval(400,400,50,50);</p> 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:

    <p>A compilation error since there is no initial value assigned to it</p> 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:

    <p>An exception that will be thrown at runtime</p> 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?

    <p>for (int i = 0; i &lt; names.length; i++)...</p> Signup and view all the answers

    Which of the following is correct? (Answer verified)

    <p>It compiles and prints out 20</p> Signup and view all the answers

    Which of the following statements would compile and execute without errors? (Answer verified)

    <p>Employee e = new Employee(); e.salary = e.age;</p> 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?

    <p>Your program will run just fine</p> Signup and view all the answers

    After executing the following piece of code, what are the colors of r1 and r2 (in that order)?

    <p>Color.blue Color.red</p> Signup and view all the answers

    What happens if MyClass.printSomething() is called from another class?

    <p>A compiler error occurs as i is not static</p> Signup and view all the answers

    Which of the following is NOT correct about accessor and mutator methods?

    <p>It is customary to start the name of Accessor and Mutator methods with Acc and Mut, respectively</p> Signup and view all the answers

    Which of the following is an incorrect statement about imperative (procedural) programming and object-oriented programming?

    <p>Imperative programming focuses on the behaviors of objects instead of the state of objects</p> Signup and view all the answers

    What method signature would be best for isSpeeding?

    <p>public boolean isSpeeding()</p> Signup and view all the answers

    Which of the following statements is false?

    <p>You must define a constructor in a class</p> Signup and view all the answers

    The ________ of an object define its potential behaviors.

    <p>Class</p> Signup and view all the answers

    Which of the following does not overload the method: public int myMethod(double a, int i)

    <p>public double myMethod(double b, int j)</p> Signup and view all the answers

    Analyze the following code. What is the output?

    <p>The program does not compile</p> Signup and view all the answers

    A variable whose meaning is confined to a method definition is called an/a

    <p>local variable</p> Signup and view all the answers

    A variable whose meaning is confined to an object of a class is called

    <p>instance variable</p> Signup and view all the answers

    The modifier private means that an instance variable can be accessed by name outside of the class definition.

    <p>False</p> Signup and view all the answers

    Which of the following statements is true about the static modifier?

    <p>A static variable is shared by all the instances of the class</p> Signup and view all the answers

    Which of the following is NOT correct about declaring a class?

    <p>It must be declared with at least one instance variable</p> Signup and view all the answers

    Consider the following code: What is the output when executed?

    <p>The code compiles and executes fine, and the output is 'Hello, Dear'</p> Signup and view all the answers

    Which of the following statements is NOT correct about constructors?

    <p>If you do not declare a constructor, then the class has no constructor, therefore cannot be instantiated</p> Signup and view all the answers

    A method that performs some actions but does not return a value can be declared a _____ method.

    <p>void</p> 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.

    <p>this</p> Signup and view all the answers

    What is the output of running the class C?

    <p>The default constructor of A is invoked and the constructor of B is invoked</p> Signup and view all the answers

    What is the output generated when Test is run considering the following code?

    <p>Animal, Horse, Donkey</p> Signup and view all the answers

    Which of the following code fragments would generate a compile error?

    <p>Test()</p> Signup and view all the answers

    Analyze the following code. What is the output?

    <p>When x.toString() is invoked, the toString() method in the Integer class is used</p> Signup and view all the answers

    What is the difference between static variable and instance variable?

    <p>Static variable is shared by all the instances of the class, while instance variable is not</p> Signup and view all the answers

    A class can be defined by extending another class. It is called ____.

    <p>inheritance</p> Signup and view all the answers

    Among the given choices, which block immediately follows a try block?

    <p>catch</p> Signup and view all the answers

    A ____ block executes regardless of whether an exception occurs or not.

    <p>finally</p> Signup and view all the answers

    Given String str = 'Java Programming!'; the following statement returns false. str == 'Java Programming!';

    <p>False</p> Signup and view all the answers

    In Java, when you open a text file you should account for a possible

    <p>FileNotFoundException</p> Signup and view all the answers

    The execution of a throw statement is referred to as _____

    <p>throwing an exception</p> Signup and view all the answers

    A runtime exception is a _____

    <p>unchecked exception</p> Signup and view all the answers

    Exceptions that are subject to the catch or declare rules are called ______

    <p>checked exceptions</p> Signup and view all the answers

    The stream that is automatically available to your Java code is _____

    <p>All of the given choices</p> Signup and view all the answers

    Given String str = 'Java Programming!'; the statement System.out.println(str.substring(5,8)); produces the output _____

    <p>rog</p> Signup and view all the answers

    The class ObjectOutputStream contains all of the following methods except:

    <p>println</p> Signup and view all the answers

    Which operator is used to concatenate two strings?

    <ul> <li></li> </ul> 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 _____

    <p>throws clause</p> 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.

    <p>False</p> Signup and view all the answers

    The output stream connected to the computer screen is

    <p>System.out</p> Signup and view all the answers

    Try blocks contain code that could possibly

    <p>throw an exception</p> Signup and view all the answers

    When the method readLine() tries to read beyond the end of a file, it returns the value of _____

    <p>-1</p> 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?

    <p>PrintWriter</p> Signup and view all the answers

    The method trim() of the String class trims of

    <p>Leading and trailing white spaces</p> Signup and view all the answers

    Tokenizing the string 'a,b;c,d;e,f' using the semi-colon delimiter results in

    <p>3 tokens</p> Signup and view all the answers

    The code new StringBuffer('abcd').reverse().substring(1,2) results in the following string;

    <p>'c'</p> Signup and view all the answers

    The catch clause has _____ parameter(s)

    <p>one</p> 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

    <p>implement the Serializable interface</p> Signup and view all the answers

    Which of the following statements is true about exception handling?

    <p>Some exceptions do not require the programmer to handle using try-catch</p> 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?

    <p>ObjectOutputStream</p> 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.

    <p>s = s.replace('J', 'j');</p> 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);

    <p>y</p> Signup and view all the answers

    Which of the following are the CORRECT ways of declaring a string?

    <p>String s = new String('Hello');</p> 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 for drawCircle.
    • 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 or get() 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.

    Quiz Team

    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!

    More Quizzes Like This

    Java Class Anatomy Quiz
    14 questions

    Java Class Anatomy Quiz

    HandsomeVariable avatar
    HandsomeVariable
    Java Class Structure Flashcards
    19 questions

    Java Class Structure Flashcards

    WellRegardedObsidian1129 avatar
    WellRegardedObsidian1129
    Java Class Writing - Lecture 11 Flashcards
    15 questions
    Use Quizgecko on...
    Browser
    Browser