Declarations and Access Control Questions and Answers

Take Exam

Declarations and Access Control form the foundation of Java programming, ensuring secure and structured code management. This section provides Java programming questions and answers focusing on classes, objects, access modifiers, and variable scope. Ideal for students preparing for TCS, Infosys, Capgemini, and Wipro technical interviews, these questions test your understanding of Java’s encapsulation and visibility rules. Each question includes explanations to reinforce key concepts. Practicing these Java MCQs with solutions PDF helps improve coding accuracy and interview performance. Whether you’re a beginner or preparing for a coding round, mastering these topics is crucial for success in programming assessments.

Declarations and Access Control

Showing 10 of 18 questions

11. What is the narrowest valid returnType for methodA in line 3? public class ReturnIt {     returnType methodA(byte x, double y) /* Line 3 */     {         return (long)x / y * 2;     } }

  • int
  • byte
  • long
  • double
Show Answer Report

12. class A {      protected int method1(int a, int b)     {         return 0;     } } Which is valid in a class that extends class A?

  • public int method1(int a, int b) {return 0; }
  • private int method1(int a, int b) { return 0; }
  • public short method1(int a, int b) { return 0; }
  • static protected int method1(int a, int b) { return 0; }
Show Answer Report

13. Which one creates an instance of an array?

  • int[ ] ia = new int[15];
  • float fa = new float[20];
  • char[ ] ca = "Some String";
  • int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };
Show Answer Report

14. Which two of the following are legal declarations for nonnested classes and interfaces?     final abstract class Test {}     public static interface Test {}     final public class Test {}     protected abstract class Test {}     protected interface Test {}     abstract public class Test {}

  • 1 and 4
  • 2 and 5
  • 3 and 6
  • 4 and 6
Show Answer Report

15. Which of the following class level (nonlocal) variable declarations will not compile?

  • protected int a;
  • transient int b = 3;
  • private synchronized int e;
  • volatile int d;
Show Answer Report

16. Which two cause a compiler error?     float[ ] f = new float(3);     float f2[ ] = new float[ ];     float[ ]f1 = new float[3];     float f3[ ] = new float[3];     float f5[ ] = {1.0f, 2.0f, 2.0f};

  • 2, 4
  • 3, 5
  • 4, 5
  • 1, 2
Show Answer Report

17. Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

  • final
  • static
  • private
  • protected
Show Answer Report

18. Which is a valid declaration within an interface?

  • public static short stop = 23;
  • protected short stop = 23;
  • transient short stop = 23;
  • final void madness(short stop);
Show Answer Report
Questions and Answers for Competitive Exams Various Entrance Test