Classes and Object in Java

This Test will cover the basic introduction to Classes, including basic syntax, initialization, Instance variables and Methods.
Q. Which of the following is not a valid declaration of a Top level class ?
Q. How can such a restriction be enforced ?
A method within a class is only accessible by classes that are defined within the same package as the class of the method. 
Q. A special method that is used to initialize a class object ?
Q. Two methods are said to be overloaded if they have,

Q. What will be the output of the program ?

class A
{
    int x = 10; 
    public void assign(int x)
    {
        x = x;  
        System.out.println(this.x);
    }
    public static void main(String[] args)
    {
        new A().assign(100);	
    }
}
Q. Which statement does not create an object of class Student{} ?
Q. this keyword in java is used to ?
Q. What is the prototype of the default constructor for given class?
public class Test { }
Q. Which method is called by Garbage collection thread just before collecting eligible Objects ?

Q. Garbage Collection in java is done by who?
Q. What will be the output of the following program?

class B
{
    static int count = 100;
    public void increment()
    {
        count++;
    }
    public static void main(String []args)
    {
        B b1 = new B();
        b1.increment();
        B b2 = new B();
        System.out.println(b2.count);   // line 13
    }
}
Q. Which of the following statement declares a constant field in Java?
Q. Given the following code, which line will generate an error ?

class Test
{
    static int x = 100;              // line 3
    int y = 200;                     // line 4 
    public static void main(String []args)
    {
        final int z;                  // line 7
        z = x + y;                    // line 8
        System.out.println(z);
    }
}
Q. What will happen if you try to compile and run the following code ?

class Test
{
    int x;
    Test(int n)
    {
        System.out.println(x=n);   // line 6
    }
  
    public static void main(String []args)
    {
        Test n = new Test();      // line 10
    }
} 
Q. Can you make a Constructor final ?

Having second thoughts for any code related question?

Try executing the code in our Code Playground