Declaration And Access Control — Part 1 MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Declaration And Access Control — Part 1 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Choose all the lines which if inserted independently instead of "//insert code here" will allow the following code to compile: public class Test{ public static void main(String args[]){ add(); add(1); add(1, 2); } // insert code here }
Correct Answer: Option E — static void add(int...args){}
Detailed Academic Explanation:
var-args = variable number of arguments = 0 or many void add(Integer... args){} is correct IF made "static" as it's called from a static context: main(). Var-args can be of both object(eg Integer) and primitive(eg int) types. static void add(int... args, int y){} is correct IF its parameters' order is reversed. If a method has both var-arg(0 or MAX 1) + non-var-args(0 or more) parameters then the var-arg parameter MUST come LAST! static void add(int args...){} : "..." must come after the type of the var-arg parameter, not after its name static void add(int[]... args){} : for this to be a correct declaration then add() should have been called something like this: "add(arr);" or "add(arr, arr);" where "arr" could be defined as "int[] arr = new int[5];" static void add(int...args){} is a valid way to define var-args (there is no need to have any space between "..." and the type and name of the var-arg param)
💡Key Conceptual Anchor:Declaration And Access Control
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of compiling and running the following code? class Base{ private Base(){ System.out.print("Base"); } } public class test extends Base{ public test(){ System.out.print("Derived"); } public static void main(String[] args){ new test(); } }
Correct Answer: Option D — Compilation Error
Detailed Academic Explanation:
Implicit super constructor Base is not visible, must explicitly invoke another constructor.
💡Key Conceptual Anchor:Declaration And Access Control
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of compiling and running the following code? public class Tester{ static int x = 4; public Tester(){ System.out.print(this.x); // line 1 Tester(); } public static void Tester(){ // line 2 System.out.print(this.x); // line 3 } public static void main(String... args){ // line 4 new Tester(); } }
Correct Answer: Option C — Compile error at line 3 (static methods can't invoke this)
Detailed Academic Explanation:
static methods can't invoke 'this'.
💡Key Conceptual Anchor:Declaration And Access Control
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the result of compiling and running the following code? public class Tester{ static int x = 4; int y = 9; public Tester(){ System.out.print(this.x); // line 1 printVariables(); } public static void printVariables(){ System.out.print(x); // line 2 System.out.print(y); // line 3 } public static void main(String... args) { // line 4 new Tester(); } }
Correct Answer: Option B — Compile error at line 3 (static methods can't make reference to non-static variables)
Detailed Academic Explanation:
static methods cannot make references to non-static variables.
💡Key Conceptual Anchor:Declaration And Access Control
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: public class InitDemo{ static int i = demo(); static{ System.out.print(i); } InitDemo(){ System.out.print("hello1"); } public static void main(String... args){ System.out.print("Hello2"); } static int demo(){ System.out.print("InsideDemo"); return 10; } }
Correct Answer: Option C — InsideDemo 10 Hello2
Detailed Academic Explanation:
As soon as the class are loaded static variables are initialized first. To initialize it demo must be called first then then static block executes and then main method is called.
💡Key Conceptual Anchor:Declaration And Access Control
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
A package is a collection of
Correct Answer: Option D — Classes and Interfaces
Detailed Academic Explanation:
Think of a package like a folder or a directory on your computer. What do you usually put in a folder? Files! In Java, packages help us organize our code. Classes and Interfaces are both types of "files" in Java. So, a package can contain both. Option A is partially correct (Classes), but it's not the whole story. Option B is also partially correct (Interfaces), but it's also not the complete picture. Option C and Option E are incorrect because "Editing tools" are not stored within the Java package. Option D is the most accurate. Therefore, the correct answer is D: Classes and Interfaces.
💡Key Conceptual Anchor:Declaration And Access Control
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
You have the following code in a file called Test.java class Base{ public static void main(String[] args){ System.out.println("Hello"); } } public class Test extends Base{} What will happen if you try to compile and run this?
Correct Answer: Option D — Compiles and runs printing
Detailed Academic Explanation:
This will compile and print "Hello". The entry point for a standalone java program is the main method of the class that is being run. The java run-time system will look for that method in class Test and find that it should have such a method. It does not matter whether it is defined in the class itself or is inherited from a parent class.
💡Key Conceptual Anchor:Declaration And Access Control
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output for the below code? public class A{ static{ System.out.println("static"); } { System.out.println("block"); } public A(){ System.out.println("A"); } public static void main(String[] args){ A a = new A(); } }
Correct Answer: Option B — static block A
Detailed Academic Explanation:
First execute static block, then statement block and then constructor.
💡Key Conceptual Anchor:Declaration And Access Control
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output? public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.println(value); } public static void changeValue(String a){ a = "xyz"; } }
Correct Answer: Option A — abc
Detailed Academic Explanation:
Java pass reference as value. passing the object reference, and not the actual object itself. Simply reassigning to the parameter used to pass the value into the method will do nothing, because the parameter is essentially a local variable.
💡Key Conceptual Anchor:Declaration And Access Control
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output for the below code? public class Test{ static{ int a = 5; } public static void main(String[] args){ System.out.println(a); } }
Correct Answer: Option A — Compile with error
Detailed Academic Explanation:
A variable declared in a static initializer is not accessible outside its enclosing block.
💡Key Conceptual Anchor:Declaration And Access Control
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Determine output: class A{ { System.out.print("b1 "); } public A(){ System.out.print("b2 "); } } class B extends A{ static{ System.out.print("r1 "); } public B(){ System.out.print("r2 "); } { System.out.print("r3 "); } static{ System.out.print("r4 "); } } public class Test extends B{ public static void main(String[] args){ System.out.print("pre "); new Test(); System.out.println("post "); } }
Correct Answer: Option C — r1 r4 pre b1 b2 r3 r2 post
Detailed Academic Explanation:
All static blocks execute first then blocks and constructor. Blocks and constructor executes (super class block then super class constructor, sub class block then sub class constructor). Sequence for static blocks is super class first then sub class. Sequence for blocks is super class first then sub class.
💡Key Conceptual Anchor:Declaration And Access Control
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output for the below code? static public class Test{ public static void main(String[] args){ char c = 'a'; switch(c){ case 65 : System.out.println("one");break; case 'a': System.out.println("two");break; case 3 : System.out.println("three"); } } }
Correct Answer: Option D — Compile error - Illegal modifier for the class Test; only public, abstract & final are permitted.
Detailed Academic Explanation:
Outer class can only declare public , abstract and final. Illegal modifier for the class Test; only public, abstract & final are permitted.
💡Key Conceptual Anchor:Declaration And Access Control
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output after compiling and running following program code? public class Test{ static int a; public static void main(String[] args){ System.out.println("one"); call(1); } static void call(int a){ this.a=10; System.out.println("two "+a); } }
Correct Answer: Option D — Compile time error.
Detailed Academic Explanation:
Static members are common for all objects, where as ‘this’ refer to a particular object. so we cant use ‘this’ operator in the static methods.
💡Key Conceptual Anchor:Declaration And Access Control
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output after the following program is compiled and executed? public class Test{ public static void main(String args[]){ int x = 10; x = myMethod(x--); System.out.print(x); } static int myMethod(final int x){ return x--; } }
Correct Answer: Option B — The program will lead to compilation error.
Detailed Academic Explanation:
While compiling the Test class, the compilation error occurs implying that the final parameter x cannot be assigned a value. Therefore the option B is correct and remaining options are incorrect.
💡Key Conceptual Anchor:Declaration And Access Control
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the default access modifier for class members in Java if no access modifier is specified?
Correct Answer: Option D — package-private
Detailed Academic Explanation:
package-private is the correct answer to “What is the default access modifier for class members in Java if no access modifier is specified”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which access modifier restricts a class member's visibility only to the same class and its subclasses?
Correct Answer: Option C — protected
Detailed Academic Explanation:
protected is the correct answer to “Which access modifier restricts a class member's visibility only to the same class and its subclasses”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What keyword is used to declare a constant in Java, which is implicitly final and static?
Correct Answer: Option A — final
Detailed Academic Explanation:
final is the correct answer to “What keyword is used to declare a constant in Java, which is implicitly final and static”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following access modifiers allows access to a class member from any class in any package?
Correct Answer: Option C — public
Detailed Academic Explanation:
public is the correct answer to “Which of the following access modifiers allows access to a class member from any class in any package”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the access level of a class member with no access modifier specified in Java?
Correct Answer: Option D — package-private
Detailed Academic Explanation:
package-private is the correct answer to “What is the access level of a class member with no access modifier specified in Java”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, which access modifier allows a method or variable to be accessed within the same package only?
Correct Answer: Option D — package-private
Detailed Academic Explanation:
package-private is the correct answer to “In Java, which access modifier allows a method or variable to be accessed within the same package only”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which keyword is used to explicitly specify that a method cannot be overridden in a subclass?
Correct Answer: Option B — final
Detailed Academic Explanation:
final is the correct answer to “Which keyword is used to explicitly specify that a method cannot be overridden in a subclass”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the default access modifier for a class in Java if no access modifier is specified?
Correct Answer: Option D — package-private
Detailed Academic Explanation:
package-private is the correct answer to “What is the default access modifier for a class in Java if no access modifier is specified”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which access modifier allows a class member to be accessed only within its own class and package?
Correct Answer: Option D — package-private
Detailed Academic Explanation:
package-private is the correct answer to “Which access modifier allows a class member to be accessed only within its own class and package”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, which access modifier allows a class member to be accessed from anywhere in the code?
Correct Answer: Option A — public
Detailed Academic Explanation:
public is the correct answer to “In Java, which access modifier allows a class member to be accessed from anywhere in the code”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What keyword is used to declare a method that can be called without creating an instance of the class?
Correct Answer: Option A — static
Detailed Academic Explanation:
static is the correct answer to “What keyword is used to declare a method that can be called without creating an instance of the class”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following access modifiers restricts a method or variable's access to the same package only?
Correct Answer: Option D — package-private
Detailed Academic Explanation:
package-private is the correct answer to “Which of the following access modifiers restricts a method or variable's access to the same package only”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, what is the purpose of the static keyword when applied to a variable?
Correct Answer: Option C — It allows access from any class.
Detailed Academic Explanation:
It allows access from any class. is the correct answer to “In Java, what is the purpose of the static keyword when applied to a variable”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which keyword is used to define a superclass's method that is overridden by a subclass method?
Correct Answer: Option C — super
Detailed Academic Explanation:
super is the correct answer to “Which keyword is used to define a superclass's method that is overridden by a subclass method”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the access level of a class member declared as private in Java?
Correct Answer: Option C — private
Detailed Academic Explanation:
private is the correct answer to “What is the access level of a class member declared as private in Java”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which access modifier allows a class member to be accessed within the same package and its subclasses?
Correct Answer: Option C — protected
Detailed Academic Explanation:
protected is the correct answer to “Which access modifier allows a class member to be accessed within the same package and its subclasses”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the keyword used to declare a class that cannot be instantiated and can only be extended?
Correct Answer: Option D — abstract
Detailed Academic Explanation:
abstract is the correct answer to “What is the keyword used to declare a class that cannot be instantiated and can only be extended”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, what access modifier is used to specify that a class member can be accessed only within its class?
Correct Answer: Option B — private
Detailed Academic Explanation:
private is the correct answer to “In Java, what access modifier is used to specify that a class member can be accessed only within its class”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is not an access modifier in Java?
Correct Answer: Option A — abstract
Detailed Academic Explanation:
abstract is the correct answer to “Which of the following is not an access modifier in Java”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What access modifier allows a class member to be accessed from any class in the same package or any subclass?
Correct Answer: Option C — public
Detailed Academic Explanation:
public is the correct answer to “What access modifier allows a class member to be accessed from any class in the same package or any subclass”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, which access modifier allows a class member to be accessed from any class, even outside the package?
Correct Answer: Option A — public
Detailed Academic Explanation:
public is the correct answer to “In Java, which access modifier allows a class member to be accessed from any class, even outside the package”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What keyword is used to declare a method that is implemented in a subclass to provide a specific implementation?
Correct Answer: Option A — abstract
Detailed Academic Explanation:
abstract is the correct answer to “What keyword is used to declare a method that is implemented in a subclass to provide a specific implementation”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In Java, what is the purpose of the final keyword when applied to a method?
Correct Answer: Option C — It makes the method constant.
Detailed Academic Explanation:
It makes the method constant. is the correct answer to “In Java, what is the purpose of the final keyword when applied to a method”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which access modifier allows a class member to be accessed from any class in the same package but not from outside?
Correct Answer: Option D — default
Detailed Academic Explanation:
default is the correct answer to “Which access modifier allows a class member to be accessed from any class in the same package but not from outside”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What keyword is used to declare a variable that can be accessed without creating an instance of the class?
Correct Answer: Option A — static
Detailed Academic Explanation:
static is the correct answer to “What keyword is used to declare a variable that can be accessed without creating an instance of the class”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which access modifier restricts a class member's visibility to only the same class, its package, and subclasses?
Correct Answer: Option D — protected
Detailed Academic Explanation:
protected is the correct answer to “Which access modifier restricts a class member's visibility to only the same class, its package, and subclasses”. This question is standard in competitive examination syllabi for Declaration And Access Control.
💡Key Conceptual Anchor:Declaration And Access Control
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.