Java Programming Fundamentals
18 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which statement accurately describes the use of the static keyword when applied to a method in Java?

  • The method belongs to the class itself rather than to any specific instance of the class. (correct)
  • The method can only be called once during the program's execution.
  • The method cannot be accessed from outside the class.
  • The method can be overridden by subclasses.

Consider the following code snippet: System.out.println(5 + 3 * 2);. What will be the output?

  • 16
  • 10
  • 13
  • 11 (correct)

In Java, a constructor can have a return type, such as int or String.

False (B)

Which access modifier restricts access of a class member to only within its own class?

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

The ______ keyword is used to create a new object in Java.

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

What is the primary purpose of the return keyword in a Java method?

<p>To return a value and/or control back to the calling method.</p> Signup and view all the answers

Which of the following is NOT a primitive data type in Java??

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

Match each term with its corresponding description regarding Object-Oriented Programming principles:

<p>Encapsulation = Bundling data and methods that operate on that data within a class, and restricting direct access to some of the object's components. Inheritance = A mechanism where a new class acquires the properties and behaviors of an existing class. Polymorphism = The ability of an object to take on many forms. This often occurs when a parent class reference is used to refer to a child class object.</p> Signup and view all the answers

Which of the following is a valid way to declare an array in Java?

<p>Both A and B (C)</p> Signup and view all the answers

What is the result of the following expression: true && false?

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

Which of the following loop types is specifically designed to execute a set number of times?

<p>for loop (C)</p> Signup and view all the answers

Which statement accurately describes a method signature in Java?

<p>Method name and parameter list (B)</p> Signup and view all the answers

The main() method can be overloaded in Java.

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

String objects in Java are mutable.

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

Explain the difference between StringBuilder and StringBuffer.

<p><code>StringBuilder</code> is not synchronized and is faster for single-threaded operations, while <code>StringBuffer</code> is synchronized and thread-safe.</p> Signup and view all the answers

What are accessor and mutator methods? Provide an example.

<p>Accessor methods (getters) retrieve the value of a field, while mutator methods (setters) modify the value of a field. Example: <code>public int getAge() { return age; }</code> and <code>public void setAge(int age) { this.age = age; }</code>.</p> Signup and view all the answers

The ______ keyword refers to the current object instance and is used to distinguish between instance variables and parameters when they have the same name.

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

Match the loop type with its appropriate use case:

<p>for loop = Iterating a known number of times while loop = Iterating until a specific condition is false do-while loop = Iterating at least once, then until a condition is false</p> Signup and view all the answers

Flashcards

System.out.println()

The correct way to display text output in Java.

Object-Oriented Programming

A fundamental style of programming based on objects, including encapsulation, inheritance, and polymorphism.

void keyword

Used in a method declaration to specify that the method does not return any value.

x++ (post-increment)

A post-increment operator increases the value of a variable by one after returning its original value.

Signup and view all the flashcards

private access modifier

Restricts access to the declaring class itself.

Signup and view all the flashcards

new keyword

Allocates memory for a new object of a class.

Signup and view all the flashcards

Valid Identifier

A valid sequence of characters used to name a variable, method, class, or other element. Must start with a letter, underscore (_), or dollar sign ($).

Signup and view all the flashcards

static keyword

Belongs to the class itself, not to any instance of the class.

Signup and view all the flashcards

Valid Array Declaration in Java?

Both int[] arr; and int arr[]; are valid array declarations in Java.

Signup and view all the flashcards

true && false

true && false evaluates to false because both conditions must be true for the entire expression to be true.

Signup and view all the flashcards

Loop for Specific Iterations

A for loop executes a specific number of times, controlled by initialization, condition, and increment/decrement.

Signup and view all the flashcards

Method Signature

A method signature includes the method name and its parameter list (types and order).

Signup and view all the flashcards

Multiple Constructors

A class can have multiple constructors with different parameter lists (method overloading).

Signup and view all the flashcards

Break Statement

The break statement exits a loop prematurely, skipping any remaining iterations.

Signup and view all the flashcards

Final Keyword

The final keyword prevents a variable from being reassigned after its initial assignment.

Signup and view all the flashcards

Accessor vs. Mutator Methods

Accessor methods (getters) retrieve the value of a field, while mutator methods (setters) modify the value of a field.

Signup and view all the flashcards

StringBuilder vs. StringBuffer

StringBuilder is not synchronized (faster, single-threaded), while StringBuffer is synchronized (thread-safe).

Signup and view all the flashcards

Primitive vs. Reference Types

Primitive types hold values directly; reference types hold references (pointers) to objects in memory.

Signup and view all the flashcards

Study Notes

Multiple Choice Questions

  • The correct syntax to print "Hello, World" in Java is System.out.println(“Hello, World”);
  • Compilation is NOT a principle of Object-Oriented Programming
  • The keyword void defines a method that does not return a value
  • The code int x = 10; System.out.println(x++); outputs 10
  • The private access modifier allows a class member to be accessible only within its own class
  • The new keyword in Java creates an object
  • firstVariable is a valid identifier in Java.
  • System.out.println(5 + 3 * 2); will output 11.
  • Constructors must have the same name as the class and can be private.
  • The static keyword indicates the method belongs to the class rather than an instance.
  • String is NOT a primitive data type in Java
  • The default value of an integer variable in Java is 0.
  • The Scanner class is used to handle user input in Java.
  • The code String str = "Hello"; str = str + " World"; System.out.println(str); outputs "Hello World".
  • The if-else structure can have multiple else if branches.
  • The return keyword returns control to the calling method and a value from the method.
  • int[] arr; and int arr[]; are valid ways to declare an array in Java.
  • The expression true && false results in false.
  • A for loop is used to create a loop that executes a specific number of times.
  • A method signature is correctly described by its method name and parameter list.

True/False Questions

  • The main() method can be overloaded in Java: True
  • A class can have multiple constructors: True
  • The assert statement can be used to test assumptions in code: True
  • String objects in Java are mutable: False
  • The break statement can be used to exit a loop prematurely: True
  • The final keyword can prevent a variable from being reassigned: True
  • The switch statement can only evaluate integer expressions: False
  • The Math.random() method returns a random integer: False
  • The StringBuffer class is synchronized and thread-safe: True
  • A method can have multiple return statements: True

Short Answer Questions

  • A while loop continues until a specified condition is false, while a for loop iterates a set number of times, based on initialization, condition, and increment/decrement expressions.
  • Accessor (getters) methods retrieve the value of a field, while mutator (setters) methods modify the value of a field.
    • Example:
      • Getter: public int getAge() { return age; }
      • Setter: public void setAge(int age) { this.age = age; }
  • The assert operator is used for debugging to make sure a condition is true at a certain point in the program.
  • StringBuilder is not synchronized and is faster for single-threaded operations, while StringBuffer is synchronized and thread-safe.
  • Example Java class:
class Car {
  private String model;
  private int year;

  public Car(String model, int year) {
    this.model = model;
    this.year = year;
  }

  public String getModel() {
    return model;
  }
}
  • The Scanner class gets input from sources like user console input, and can parse primitive types and strings.
  • Method overloading allows multiple methods with the same name but different parameters (types, number, or both) to exist in the same class.
    • Example:
    public int add(int a, int b) { return a + b; }
    public double add(double a, double b) { return a + b; }
    
  • The this keyword refers to the current object instance and distinguishes between instance variables and parameters having the same name.
  • The software engineering life cycle phases: Requirements Analysis, Design, Implementation, Testing, Deployment, and Maintenance.
  • Primitive types are basic data types (like int, char, boolean) holding values directly, while reference types (like String, Arrays, Objects) hold references to the actual data in memory.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Test your knowledge of introductory Java programming concepts. This quiz covers syntax, object-oriented principles, data types, and basic operations. Review access modifiers, constructors, and user input handling in Java.

More Like This

Java Syntax and History
3 questions
Java Syntax and Object-Oriented Programming
9 questions
Object-oriented Programming in Java
9 questions
Use Quizgecko on...
Browser
Browser