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.

Array & String - Practice Quiz

11. Which of the following is an arithmetic operator in Java?

  • &&
  • % (Correct Answer)
  • ||
  • !

12. What is the purpose of conditional operators in Java?

  • To perform arithmetic operations.
  • To evaluate conditions and produce boolean results.
  • To perform bitwise operations.
  • To perform type casting.

13. Which of the following is a logical operator in Java?

  • +
  • ^
  • *
  • ! (Correct Answer)

14. What is the purpose of bitwise operators in Java?

  • To perform arithmetic operations.
  • To evaluate conditions and produce boolean results.
  • To perform logical operations.
  • To perform operations on individual bits of integers.

15. What is the purpose of the arithmetic operators in Java?

  • To perform operations on individual bits of integers.
  • To evaluate conditions and produce boolean results.
  • To perform logical operations.
  • To perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

Core Java - Graded Quiz

16. Which of the following is a control flow statement in Java?

  • int
  • switch
  • String
  • System.out.println()

17. What is the purpose of the ternary operator in Java?

  • To perform loops.
  • To perform arithmetic operations.
  • To evaluate conditions and produce results based on the outcome of the condition.
  • To declare variables.

18. Which of the following is a loop in Java?

  • continue
  • switch
  • for-each
  • for

19. What is the purpose of the break statement in Java?

  • To end the program execution.
  • To skip the current iteration of a loop and move to the next iteration.
  • To continue to the next iteration of a loop.
  • To exit a loop or switch statement before its normal completion.

20. What is the purpose of the continue statement in Java?

  • To end the program execution.
  • To skip the next iteration of a loop and move to the iteration after the next.
  • To exit a loop or switch statement before its normal completion.
  • To skip the current iteration of a loop and move to the next iteration.

21. What is the purpose of the exit statement in Java?

  • To skip the current iteration of a loop and move to the next iteration.
  • To end the program execution.
  • To exit a loop or switch statement before its normal completion.
  • There is no exit statement in Java.

22. Which of the following is a valid use of the switch statement in Java?

  • Switching on a boolean variable.
  • Switching on a float variable.
  • Switching on a String variable.
  • Switching on an object reference variable.

23. What is the purpose of the for loop in Java?

  • To declare variables.
  • To perform arithmetic operations.
  • To perform conditional branching.
  • To repeatedly execute a block of code based on a condition.

24. What is the purpose of the while loop in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To repeatedly execute a block of code based on a condition.
  • To declare variables.

25. What is the purpose of the for-each loop in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To declare variables.
  • To iterate over elements of an array or a collection.

26. What is the purpose of the break statement in Java?

  • To terminate the program execution.
  • To skip the current iteration of a loop and move to the next iteration.
  • To continue with the next iteration of a loop.
  • To resume execution after the end of a loop.

27. What is the purpose of the continue statement in Java?

  • To terminate the program execution.
  • To skip the current iteration of a loop and move to the next iteration.
  • To exit a loop or switch statement before its normal completion.
  • To resume execution after the end of a loop.

28. What is the purpose of the ternary operator in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To declare variables.
  • To iterate over elements of an array or a collection.

29. What is the purpose of the control flow statements in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To declare variables.
  • To iterate over elements of an array or a collection.

30. What is the purpose of the switch case statement in Java?.

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To declare variables.
  • To iterate over elements of an array or a collection.

31. What is the purpose of the for loop in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To iterate over a collection of elements a specific number of times.
  • To declare variables.

32. What is the purpose of the while loop in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To iterate over a collection of elements as long as a condition is true.
  • To declare variables.

33. What is the purpose of the for-each loop in Java?

  • To perform arithmetic operations.
  • To perform conditional branching.
  • To iterate over elements of an array or a collection.
  • To declare variables.

34. What is the purpose of the break statement in Java?

  • To terminate the program execution.
  • To skip the current iteration of a loop and move to the next iteration.
  • To exit a loop or switch statement before its normal completion.
  • To resume execution after the end of a loop.

35. What is the purpose of the continue statement in Java?

  • To terminate the program execution.
  • To skip the current iteration of a loop and move to the next iteration.
  • To exit a loop or switch statement before its normal completion.
  • To resume execution after the end of a loop.

Leave a Reply