42. Choose the correct statement
public class Circle{
private double radius;
public Circle(double radius){
radius = radius;
}
}
public class Circle{
private double radius;
public Circle(double radius){
radius = radius;
}
}
43. Choose the correct statement. Restriction on static methods are: I. They can only call other static methods.
II. They must only access static data.
III. They cannot refer this or super in any way.
II. They must only access static data.
III. They cannot refer this or super in any way.
44. 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?
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?
45. What will be the output?
public class Test{
static{
int a = 5;
}
public static void main(String args[]){
new Test().call();
}
void call(){
this.a++;
System.out.print(this.a);
}
}
public class Test{
static{
int a = 5;
}
public static void main(String args[]){
new Test().call();
}
void call(){
this.a++;
System.out.print(this.a);
}
}
46. Determine Output:
class MyClass{
static final int a = 20;
static final void call(){
System.out.println("two");
}
static{
System.out.println("one");
}
}
public class Test{
public static void main(String args[]){
System.out.println(MyClass.a);
}
}
class MyClass{
static final int a = 20;
static final void call(){
System.out.println("two");
}
static{
System.out.println("one");
}
}
public class Test{
public static void main(String args[]){
System.out.println(MyClass.a);
}
}
47. 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();
}
}
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();
}
}
48. 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";
}
}
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";
}
}
49. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
50. 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);
}
}
public class Test{
static{
int a = 5;
}
public static void main(String[] args){
System.out.println(a);
}
}