zuai-logo

What is 'scope' in programming?

Scope determines where a variable can be used within your code.

Flip to see [answer/question]
Flip to see [answer/question]

All Flashcards

What is 'scope' in programming?

Scope determines where a variable can be used within your code.

Define 'local scope'.

Variables declared inside a method or constructor have local scope; accessible only within that block.

Define 'global scope'.

Variables declared outside any method or constructor have global scope; accessible throughout the class.

What are 'access modifiers'?

Access modifiers control the visibility and accessibility of variables and methods from outside the class.

Define 'private' access modifier.

Private members are only accessible within the class where they are declared.

Define 'package' (default) access.

Accessible by other classes within the same package (folder).

Define 'protected' access.

Accessible by classes in the same package and by subclasses in other packages.

Define 'public' access.

Public members are accessible from any class.

What is an 'instance variable'?

A non-static variable belonging to an object.

Define 'encapsulation'.

Bundling data (variables) and methods that operate on the data into a single unit (class), and hiding the internal details from the outside world.

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 are the key differences between private and public access modifiers?

Private: accessible only within the class. Public: accessible from any class.

Compare local and global variables in terms of scope.

Local: limited to the block they are defined in. Global: accessible throughout the class.

What are the differences between 'package' and 'protected' access modifiers?

Package: accessible within the same package. Protected: accessible within the same package and by subclasses in other packages.

Contrast the use cases of public instance variables vs. private instance variables with public getter/setter methods.

Public instance variables: simpler but violate encapsulation. Private with getters/setters: more complex but allows controlled access and modification.

Compare the scope of static vs. instance variables.

Static: class-level scope, shared by all instances. Instance: object-level scope, unique to each instance.

Differentiate between access modifiers and scope.

Access modifiers: control visibility from outside the class. Scope: determines where a variable is accessible within the code.

Compare the accessibility of a variable declared without any access modifier versus one declared as public.

No modifier (package-private): accessible only within the same package. public: accessible from anywhere.

Contrast the impact of using private vs. protected members in the context of inheritance.

private: not accessible to subclasses. protected: accessible to subclasses, allowing for controlled inheritance.

Compare the use of local variables inside a method versus parameters passed to the method.

Local variables: declared and initialized within the method. Parameters: passed into the method from the caller.

Contrast the scope of a variable declared inside an 'if' statement block versus a variable declared outside the 'if' statement but within the same method.

Inside 'if': scope limited to the 'if' block. Outside 'if': scope extends to the entire method (excluding shadowed regions).