Java Main Method Quiz
30 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

What is the primary purpose of the main method in a Java program?

  • To handle exceptions
  • To declare user-defined functions
  • To initialize variables
  • To execute the program (correct)

Which of the following is the correct way to declare the main method in a Java program?

  • void main(String args)
  • public void main(String[] args)
  • public static main(String args)
  • public static void main(String[] args) (correct)

Which of the following is not a primitive type in Java?

  • double
  • int
  • float
  • String (correct)

What distinguishes dynamically typed languages from statically typed languages?

<p>Dynamically typed languages allow for flexible variable types. (D)</p> Signup and view all the answers

Which of the following accurately describes the purpose of reference types in Java?

<p>To store pointers to memory locations (C)</p> Signup and view all the answers

If a Java program has errors in the main method declaration, what issue is likely to occur?

<p>Syntax errors during compilation (A)</p> Signup and view all the answers

What will be printed if myNum is 15 in the given Java code snippet?

<p>Your number is greater than 10 (A)</p> Signup and view all the answers

Which of the following statements about the Vehicle class is true?

<p>The Vehicle class has a constructor that initializes type and year (A)</p> Signup and view all the answers

Why does encapsulation play a significant role in Java programming?

<p>To prevent unauthorized access and modifications to data (C)</p> Signup and view all the answers

Which of the following best describes encapsulation in Java?

<p>'Encapsulation' refers to grouping variables and methods within a class (C)</p> Signup and view all the answers

In Java, what is the purpose of getters and setters?

<p>To provide controlled access to class variables (D)</p> Signup and view all the answers

Which of the following best describes the role of constructors in Java?

<p>Constructors are used to initialize objects (B)</p> Signup and view all the answers

What is the main purpose of encapsulation in Object-Oriented Programming?

<p>Enhancing modularity and code reusability (C)</p> Signup and view all the answers

Which method in the Car class is responsible for checking if a car needs maintenance based on its mileage?

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

What is the purpose of the setYear() method in the Car class?

<p>To set the car's year of manufacture (C)</p> Signup and view all the answers

What does the getMaintenance() method output when a car's mileage is less than or equal to 100,000 miles?

<p>'No maintenance required at this time.' (D)</p> Signup and view all the answers

Which keyword in Java is used by a subclass to inherit methods and variables from its superclass?

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

Which method in the Car class allows access to the make attribute of a Car object?

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

Which statement about primitive types in Java is correct?

<p>Primitive types are variable types in Java that store a value directly. (D)</p> Signup and view all the answers

In the provided Java code snippet, what is the correct way to read user input?

<p>int myNum = scn.nextInt(); (A)</p> Signup and view all the answers

Which of the following is a type of loop not found in Java?

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

Which line of code correctly creates an ArrayList of integers in Java?

<p>ArrayList myList = new ArrayList(); (C)</p> Signup and view all the answers

What is the correct conditional statement in Java to check if myNum is greater than 10?

<p>if (myNum &gt; 10) { (B)</p> Signup and view all the answers

Which of the following correctly prints both a number and its square in Java?

<p><code>System.out.println(&quot;Your number is: &quot; + myNum + &quot; Its square is: &quot; + square);</code> (C)</p> Signup and view all the answers

What does polymorphism allow in Java?

<p>Objects of different classes to be treated as objects of a common superclass (B)</p> Signup and view all the answers

Which method needs to be overridden in the subclasses Cat and Dog?

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

What are the instance variables in the Animal class?

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

Which concept in Java allows for method overloading?

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

What is the purpose of the Animal reference variables in the practical question?

<p>To store any object references (B)</p> Signup and view all the answers

What happens when the makeSound() method is called on an Animal reference variable pointing to a Dog object?

<p>It prints the sound of a Dog (D)</p> Signup and view all the answers

More Like This

Java Basics Quiz 1
12 questions

Java Basics Quiz 1

AccommodativeTelescope avatar
AccommodativeTelescope
Android Main Activity Logic Quiz
18 questions
Introduction à la Programmation Java
53 questions
Java Programming Basics Quiz
47 questions

Java Programming Basics Quiz

ConscientiousSaxhorn avatar
ConscientiousSaxhorn
Use Quizgecko on...
Browser
Browser