fundamental of java programming coursera week 2 quiz answers

Variables and Data Types - Practice Quiz

1. What is the purpose of using packages in Java?

  • To organize and group related classes and interfaces.
  • To define variables and methods.
  • To create objects.
  • To specify the visibility of classes and interfaces.

2. Which of the following is the correct syntax for defining a package in Java?

  • package MyPackage;
  • define MyPackage;
  • pkg MyPackage;
  • package MyPackage { }

3. Which of the following is NOT a type of comment in Java?

  • // Single-line comment
  • /* Multi-line comment /
  • <!– HTML comment –>
  • /** Javadoc comment /

4. What is the purpose of clean code design in Java?

  • To make the code look visually appealing.
  • To reduce the size of the compiled code.
  • To improve the performance of the code.
  • To write code that is easy to understand, maintain, and extend.

5. What is the basic structure of a Java program?

  • Package declaration, import statements, class definition, main method
  • Class definition, package declaration, importstatements, main method
  • Main method, package declaration, import statements, class definition
  • Import statements, class definition, main method, package declaration

Operators - Practice Quiz

6. What are the primitive data types in Java?

  • int, float, boolean, char
  • String, double, boolean, byte
  • Integer, float, boolean, char
  • int, double, Boolean, char

7. What is type casting in Java?

  • Converting a reference data type to a primitive data type.
  • Converting a primitive data type to a reference data type.
  • Converting a value of one data type to another data type.
  • Converting a boolean data type to an integer data type.

8. What is the purpose of the "final" keyword in Java?

  • It indicates that a class cannot be inherited.
  • It indicates that a variable cannot be changed after initialization.
  • It indicates that a method cannot be overridden in a subclass.
  • All of the above.

9. What is a static variable in Java?

  • A variable that can be accessed only within the same class.
  • A variable that can be accessed only within the same method.
  • A variable that can be accessed from any class in the same package.
  • A variable that is shared among all instances of a class.

10. How do you take user input in Java?

  • By using the System.out.println() method.
  • By using the Scanner class.
  • By using the BufferedReader class.
  • By using the BufferedReader class.

Leave a Reply