What is the scope of 'x' in: public void method() { int x = 5; }?
Local scope; only accessible within the method() method.
Flip to see [answer/question]
Flip to see [answer/question]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
What is the scope of 'x' in: public void method() { int x = 5; }?
Local scope; only accessible within the method() method.
Given: private int age; can another class directly access age?
No, age is private and only accessible within its own class.
java
public class Test {
private int x = 10;
public void printX() {
System.out.println(x);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x); // What happens here?
}
}
Compile-time error: x is private and cannot be accessed from Main.
java
public class A {
int x = 5; // package access
}
public class B {
public static void main(String[] args) {
A a = new A();
System.out.println(a.x); // When is this valid?
}
}
Valid only if A and B are in the same package.
java
public class Parent {
protected int value = 10;
}
public class Child extends Parent {
public void printValue() {
System.out.println(value);
}
}
Is value accessible in Child?
Yes, value is protected and accessible in subclasses.
java
public class Example {
public int x;
public Example(int x) {
this.x = x;
}
public static void main(String[] args) {
Example e = new Example(5);
System.out.println(e.x); // What is the output?
}
}
5
java
public class Test {
private static int count = 0;
public Test() {
count++;
}
public static int getCount() {
return count;
}
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
System.out.println(Test.getCount()); // What is the output?
}
}
2
java
public class ScopeTest {
int x = 10;
public void methodA() {
int x = 20;
System.out.println(x); // What is the output?
}
public static void main(String[] args) {
ScopeTest st = new ScopeTest();
st.methodA();
}
}
20
java
public class AccessTest {
private int secret = 5;
public int getSecret() {
return secret;
}
public static void main(String[] args) {
AccessTest at = new AccessTest();
System.out.println(at.secret); // Will this compile?
}
}
No, it will not compile. secret is private and cannot be accessed directly from main.
java
public class Test {
public static void main(String[] args) {
int x = 5;
if (true) {
int x = 10; // Is this allowed?
System.out.println(x);
}
System.out.println(x);
}
}
Yes, it's allowed. The inner x shadows the outer x within the if block. Output: 10, 5
What is the significance of local scope?
It limits variable access, preventing unintended modifications and promoting modularity.
What is the significance of global scope?
It allows variables to be accessed from anywhere in the class, but can increase the risk of unintended side effects.
Why are instance variables often declared private?
To encapsulate data and control access to the object's state.
What is the purpose of access modifiers in object-oriented programming?
To control the visibility and accessibility of class members, enforcing encapsulation and abstraction.
Explain the concept of data hiding.
Restricting access to the internal representation of an object, preventing direct manipulation from outside the class.
What happens when a local variable has the same name as a global variable?
The local variable takes precedence within its scope, shadowing the global variable.
How does inheritance relate to the 'protected' access modifier?
Protected members are accessible by subclasses, enabling inheritance while still providing some level of encapsulation.
What is the role of the 'public' access modifier in class design?
It defines the interface of the class, specifying which methods and constructors are accessible from other classes.
Why is understanding scope important for debugging?
Knowing the scope of variables helps identify where a variable's value might be unexpectedly changed or accessed.
How do static variables relate to scope and access?
Static variables have class-level scope and are shared by all instances of the class. Their access is controlled by access modifiers.
How is encapsulation achieved using access modifiers?
By declaring instance variables as private and providing public getter and setter methods.
Give an example of when to use a private static variable.
For constants or counters that should only be accessed and modified within the class.
How do access modifiers support information hiding?
By restricting access to internal data and implementation details, preventing external classes from directly manipulating them.
In what scenario would you use the 'protected' access modifier?
When you want to allow subclasses in other packages to access a member but prevent access from non-related classes.
How can proper use of scope prevent naming conflicts?
By limiting the visibility of variables, reducing the chance of accidentally using the same name for different variables in different parts of the code.
How does the concept of scope apply to method parameters?
Method parameters have local scope within the method, allowing the method to operate on specific inputs without affecting variables outside the method.
Explain how access modifiers contribute to creating a stable and maintainable API.
By defining a clear interface with public members and hiding implementation details with private members, changes to the internal implementation do not affect external code that uses the API.
How can access modifiers and scope be used to implement a singleton pattern?
By making the constructor private and providing a public static method to access the single instance, controlling instantiation and access.
How can access modifiers be used to create immutable objects?
By declaring all instance variables as private and not providing any setter methods, preventing modification of the object's state after creation.
How does the use of access modifiers and scope contribute to code reusability?
By creating well-defined interfaces and encapsulating implementation details, classes can be reused in different contexts without unintended side effects or dependencies.