Bird
0
0

Which of the following is the correct syntax to declare a primitive int and a reference String in Java?

easy📝 Syntax Q3 of 15
Java - Memory Management Basics
Which of the following is the correct syntax to declare a primitive int and a reference String in Java?
Aint number = new int(5); String text = "Hello";
Bint number = 5; String text = new String("Hello");
Cint number = new int(5); String text = new String(Hello);
Dint number = Integer(5); String text = "Hello";
Step-by-Step Solution
Solution:
  1. Step 1: Declare primitive int

    Primitive int is declared simply as int number = 5; no new keyword needed.
  2. Step 2: Declare reference String

    String is a reference type; it can be assigned directly or with new String("Hello"). int number = 5; String text = new String("Hello"); uses new String("Hello") correctly.
  3. Final Answer:

    int number = 5; String text = new String("Hello"); -> Option B
  4. Quick Check:

    Primitive int direct, String reference with new [OK]
Quick Trick: Primitive types no new; reference types can use new [OK]
Common Mistakes:
  • Using new keyword with primitives
  • Incorrect String constructor usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes