0% found this document useful (0 votes)
69 views3 pages

What Would Be The Result of Attempting To Compile and Run The Following Program?

The program will fail to compile, since the static method main() cannot have a call to the non-static method func(). The program attempts to call the non-static method func() from the static main() method, which is not allowed.

Uploaded by

crazz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views3 pages

What Would Be The Result of Attempting To Compile and Run The Following Program?

The program will fail to compile, since the static method main() cannot have a call to the non-static method func(). The program attempts to call the non-static method func() from the static main() method, which is not allowed.

Uploaded by

crazz1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

What would be the result of attempting to compile and run the following

program?
class MyClass {
static MyClass ref;
String[] arguments;
public static void main(String[] args) {
ref = new MyClass();
ref.func(args);
}
public void func(String[] args) {
ref.arguments = args;
}
}
The program will fail to compile, since the static method main() cannot have a call to
the non-static method func().
The program will fail to compile, since the non-static method func() cannot access
the static variable ref.
The program will fail to compile, since the argument args passed to the static
method main() cannot be passed on to the non-static method func().
The program will compile and run successfully.
Topic: Access Modifiers
package com.test.work;
public class A {
public void m1() {System.out.print("A.m1, ");}
protected void m2() {System.out.print("A.m2, ");}
private void m3() {System.out.print("A.m3, ");}
void m4() {System.out.print("A.m4, ");}
}
class B {

public static void main(String[] args) {


A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
Assume that the code appears in a single file named A.java.
What is the result of attempting to compile and run the program?

Prints: A.m1, A.m2, A.m3, A.m4,


Compile-time error at 2.
Compile-time error at 3.
Compile-time error at 4.
Topic: Access Modifiers
Given the following source code, which comment line can be uncommented
without introducing errors?
abstract class MyClass {
abstract void f();
final void g() {}
// final void h() {}

// (1)

protected static int i;


private int j;
}
final class MyOtherClass extends MyClass {
// MyOtherClass(int n) { m = n; }
public static void main(String[] args) {
MyClass mc = new MyOtherClass();

// (2)

}
void f() {}
void h() {}
// void k() { i++; }

// (3)

// void l() { j++; }

// (4)

int m;
}

final void h() {}


MyOtherClass(int n) { m = n; }

// (1)
// (2)

void k() { i++; }

// (3)

void l() { j++; }

// (4)

You might also like