SOME QUESTIONS ON JAVA

SOME QUESTIONS ON JAVA

1.Consider the two methods (within the same class)

 public static int foo(int a, String s)

 {

 s = “Yellow”;

 a=a+2;

 return a;

 }

 public static void bar()

 {

 int a=3;

 String s = “Blue”;

 a = foo(a,s);

 System.out.println(“a=”+a+” s=”+s);

 }

 public static void main(String args[])

 {

 bar();

 }

What is printed on execution of these methods?

(a) a = 3 s = Blue       (b) a = 5 s = Yellow

(c) a = 3 s = Yellow    (d) a = 5 s = Blue (e) none of the above

2.Consider the following code fragment

 Rectangle r1 = new Rectangle();

 r1.setColor(Color.blue);

 Rectangle r2 = r1;

 r2.setColor(Color.red);

 After the above piece of code is executed, what are the colors of r1

and

 r2 (in this order)?

(a) Color.blue Color.red

(b) Color.blue Color.blue

(c) Color.red Color.red

(d) Color.red Color.blue

(e) None of the above.

3.What is printed by the following statement?

System.out.print(“Hello,\nworld!”);

(a) Hello, \nworld! (b) Hello, world!

(c) Hello,

     world!

(d) “Hello, \nworld!” (e) None of the above

4.Which of the following variable declaration would NOT compile in a java

program?

(a) int var; (b) int VAR; (c) int var1; (d) int

var_1; (e) int 1_var;.

5.Which of the following is TRUE?

(a) In java, an instance field declared public generates a compilation error.

(b) int is the name of a class available in the package java.lang

(c) Instance variable names may only contain letters and digits.

(d) A class has always a constructor (possibly automatically supplied by the

java compiler).

(e) The more comments in a program, the faster the program runs.

6.. A constructor

(a) Must have the same name as the class it is declared within.

(b) Is used to create objects.

(c) May be declared private

(d) Both (A) and (B) above

(e) (a), (b) and (c) above

7.To instantiate MyClass, you would write?

(a) MyClass mc = new MyClass();

(b) MyClass mc = MyClass();

(c) MyClass mc = MyClass;

(d) MyClass mc = new MyClass;

8.What is byte code in the context of Java?

(a) The type of code generated by a Java compiler.

(b) The type of code generated by a Java Virtual Machine.

(c) It is another name for a Java source file.

(d) It is the code written within the instance methods of a class.

(e) It is another name for comments written within a program.

9.What is garbage collection in the context of Java?

(a) The operating system periodically deletes all the java files available on the

system.

(b) Any package imported in a program and not used is automatically deleted.

(c) When all references to an object are gone, the memory used by the object is

automatically reclaimed.

(d) The JVM checks the output of any Java program and deletes anything that

doesn’t make sense.

(e) Janitors working for Sun Micro Systems are required to throw away any

Microsoft documentation found in the employees’ offices.

10.Which of the following statements is true about the Java code: car.drive(speed)?

(a) car must be an instance of the Car class

(b) speed must be an object of the Speed class

(c) drive must be a static method in the Car class

(d) drive must return a value

(e) car.drive(speed) is not a valid Java statement

11.Which of the following methods is automatically called by the Java runtime system when an object is about to be garbage collected?

(a) constructor()

(b) main()

(c) equals()

(d) finalize()

(e) toString()

12.The correct order of the declarations in a Java program is,

(a) Package declaration, import statement, class declaration

(b) Import statement, package declaration, class declaration

(c) Import statement, class declaration, package declaration

(d) Class declaration, import statement, package declaration

(e) Class declaration, package declaration, import statement.

13.An overloaded method consists of,

(a) The same method name with different types of parameters

(b) The same method name with different number of parameters

(c) The same method name and same number and type of parameters with

different return type

(d) Both (a) and (b) above

(e) (a), (b) and (c) above.

14.A protected member can be accessed in,

(a) a subclass of the same package (b) a non-subclass of the same package

(c) a non-subclass of different package (d) a subclass of different package

(e) the same class.

Which is the false option?

15.Which of the following is the correct syntax for declaring a variable in Java?

(a) var myVariable = 10;

(b) int myVariable = 10;

(c) myVariable = 10;

(d) variable myVariable = 10;

16.Which of the following is not a primitive data type in Java?

(a) byte

(b) int

(c) double

(d) string

17.Which of the following is used to create an object in Java?

(a) new

(b) create

(c) build

(d) construct

18.What is encapsulation in Java?

(a) A way of hiding the implementation details of a class from other objects

(b) A way of making a class inherit from another class

(c) A way of overriding a method in a superclass

(d) A way of implementing multiple interfaces in a class

19.What is polymorphism in Java?

(a) The ability of an object to take on many forms

(b) The ability of a class to inherit from multiple classes

(c) The ability of an interface to be implemented by multiple classes

(d) The ability of a class to be abstract

20.What is the difference between a class and an object in Java?

(a) A class is a blueprint for creating objects, while an object is an instance of a class

(b) A class is an instance of an object, while an object is a blueprint for creating classes

(c) A class and an object are the same thing in Java

(d) A class is used for encapsulation, while an object is used for inheritance

21.What is a constructor in Java?

(a) A method that is called when an object is created

(b) A method that is used for inheritance

(c) A method that is used for encapsulation

(d) A method that is used for polymorphism

22.What is the default access modifier in Java?

(a) public

(b) private

(c) protected

(d) package-private

23.What is a package in Java?

(a) A group of classes that share a common namespace

(b) A way of accessing static variables

(c) A way of implementing multiple inheritance

(d) A way of overriding methods in a superclass

24.What is the syntax for writing to a file in Java?

(a) FileWriter fileWriter = new FileWriter(“filename.txt”); fileWriter.write(“Hello, world!”); fileWriter.close();

(b) File file = new File(“filename.txt”); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(“Hello, world!”); writer.close();

(c) PrintWriter writer = new PrintWriter(“filename.txt”); writer.println(“Hello, world!”); writer.close();

(d) All of the above

25.What is an array in Java?

(a) A way of storing multiple values of the same type in a single variable

(b) A way of implementing multiple inheritance

(c) A way of overriding methods in a superclass

(d) A way of creating objects

Answer: (a)

26.What is the syntax for declaring an array in Java?

(a) int[] myArray = {1, 2, 3};

(b) int myArray = {1, 2, 3};

(c) int[] myArray = new int[3]{1, 2, 3};

(d) All of the above

27.What is the output of the following Java code?

int x = 5;

System.out.println(++x);

(a) 5

(b) 6

(c) Compile-time error

(d) Run-time error

28.Which of the following is an example of a primitive data type in Java?

(a) String

(b) Integer

(c) Double

(d) Boolean

29.What is the purpose of a constructor in Java?

(a) To initialize an object of a class

(b) To declare variables of a class

(c) To define methods of a class

(d) To import packages in a class

30.What is the output of the following Java code?

int[] arr = new int[3];

System.out.println(arr[1]);

(a) 0

(b) 1

(c) Compile-time error

(d) Run-time error

31.What is the difference between a while loop and a do-while loop in Java?

(a) There is no difference between them

(b) A while loop executes at least once, while a do-while loop may not execute at all

(c) A while loop always executes at least twice, while a do-while loop may not execute at all

(d) A while loop executes until a condition is false, while a do-while loop executes until a condition is true

32.What is the output of the following Java code?

int x = 10;

if(x > 5 && x < 15)

System.out.println(“True”);

else

System.out.println(“False”);

(a) True

(b) False

(c) Compile-time error

(d) Run-time error

33.What is the difference between a public and private access modifier in Java?

(a) A public method can be accessed from anywhere in the program, while a private method can only be accessed within the same class

(b) A public method can only be accessed within the same class, while a private method can be accessed from anywhere in the program

(c) A public method can only be accessed within the same package, while a private method can be accessed from anywhere in the program

(d) A public method can be accessed from anywhere in the program, while a private method can only be accessed within the same package

34.What is the output of the following Java code?

int[] arr = {1, 2, 3};

for(int i = 0; i < arr.length; i++)

System.out.println(arr[i]);

(a) 1 2 3

(b) 3 2 1

(c) Compile-time error

(d) Run-time error

35.Which keyword is used to define a class in Java?

(a) new

(b) class

(c) public

(d) void

36.Which of the following is not a valid identifier in Java?

(a) _myVar

(b) 2ndVar

(c) myVar

(d) My_Var

37.What is the output of the following code snippet?

int x = 5;

System.out.println(x++);

(a) 6

(b) 5

(c) 4

(d) Compiler error

38.Which keyword is used to declare a method in Java?

(a) method

(b) void

(c) public

(d) static

39.Which of the following is a valid constructor declaration in Java?

(a) void myConstructor(int x, int y) {}

(b) myConstructor(int x, int y) {}

(c) public void myConstructor(int x, int y) {}

(d) public myConstructor(int x, int y) {}

40.Which of the following access modifiers allows a member to be accessed only within its own class and any subclasses?

(a) public

(b) private

(c) protected

(d) package-private

41.What is the purpose of encapsulation in Java?

(a) To make code more efficient

(b) To improve readability of code

(c) To restrict access to certain members of a class

(d) To make code more object-oriented

42.Which of the following is an example of polymorphism in Java?

(a) Method overloading

(b) Method overriding

(c) Both (a) and (b)

(d) None of the above

43.Which keyword is used to import a package in Java?

(a) import

(b) package

(c) use

(d) include

44.Which of the following is the correct way to declare a package in Java?

(a) package myPackage;

(b) myPackage;

(c) package myPackage

(d) None of the above

45.Which keyword is used to write data to a file in Java?

(a) output

(b) write

(c) print

(d) None of the above

46.Which of the following is used to read data from a file in Java?

(a) FileRead

(b) BufferedReader

(c) FileReader

(d) All of the above

47.What is the purpose of the “super” keyword in Java?

(a) To call a superclass constructor

(b) To access a superclass method or variable

(c) To define a new class

(d) To create a new instance of a class

48.What is the purpose of the “this” keyword in Java?

(a) To refer to the current class instance

(b) To call a superclass constructor

(c) To access a superclass method or variable

(d) To create a new instance of a class

49.Which of the following is not a valid modifier in Java?

(a) private

(b) public

(c) static

(d) final

(e) constant

50.Which keyword is used to declare a method in Java?

(a) public

(b) method

(c) void

(d) class

51.What is the purpose of a constructor in Java?

(a) To create a new instance of a class

(b) To access a superclass method or variable

(c) To define a new class

(d) To initialize the state of a new object

52.What is the difference between an abstract class and an interface in Java?

(a) An interface can have method implementations, while an abstract class cannot.

(b) An abstract class can have method implementations, while an interface cannot.

(c) An abstract class can only be extended, while an interface can be implemented by any class.

(d) An interface can only be extended, while an abstract class can be implemented by any class.

53.What is the purpose of the “static” keyword in Java?

(a) To create a new instance of a class

(b) To define a new class

(c) To declare a constant variable

(d) To indicate that a variable or method belongs to the class, rather than to any individual instance of the class

54.Which loop construct is used to iterate over the elements of an array in Java?

(a) for loop

(b) while loop

(c) do-while loop

(d) enhanced for loop

55.What is the syntax for declaring an array in Java?

(a) int[] arrayName = new int[size];

(b) int arrayName[] = new int[size];

(c) Both (a) and (b) are correct.

(d) Neither (a) nor (b) are correct.

56.What is the syntax for declaring a multidimensional array in Java?

(a) int[][] arrayName = new int[size1][size2];

(b) int[] arrayName[] = new int[size1][size2];

(c) Both (a) and (b) are correct.

(d) Neither (a) nor (b) are correct.

57.What is the syntax for creating a new object in Java?

(a) ClassName objectName = new ClassName();

(b) objectName = new ClassName();

(c) new ClassName() = objectName;

(d) objectName = ClassName();

58.What is the syntax for declaring a variable in Java?

(a) variableType variableName;

(b) variableName variableType;

(c) variableType variableName = initialValue;

(d) variableName = initialValue;

59.What is the syntax for declaring a constant in Java?

(a) final variableType variableName;

(b) variableType final variableName;

(c) final variableType variableName = initialValue;

(d) variableName = final initialValue;

60.What is the syntax for using an if statement in Java?

(a) if (condition) { // code to execute }

(b) if (condition) // code to execute

(c) if {condition} // code to execute

(d) if [condition] { // code to execute }

What are the output of the following codes?

61.class Test {

    int a = 10;

    public static void main(String[] args) {

        Test obj1 = new Test();

        Test obj2 = obj1;

        obj2.a = 20;

        System.out.println(obj1.a);

    }

}

(a) 10

(b) 20

(c) 0

(d) Compilation error

62.class Test {

    public static void main(String[] args) {

        int arr[] = new int[5];

        System.out.println(arr[0]);

    }

}

(a) 0

(b) 5

(c) Compilation error

(d) Runtime error

63.class Test {

    public static void main(String[] args) {

        String str = “Hello World”;

        System.out.println(str.charAt(0));

    }

}

(a) H

(b) e

(c) l

(d) o

64.class Test {

    public static void main(String[] args) {

        String str = “Java is awesome”;

        System.out.println(str.substring(0, 4));

    }

}

(a) Jav

(b) Java

(c) is a

(d) Compilation error

65.class Test {

    public static void main(String[] args) {

        String str1 = “Hello”;

        String str2 = “World”;

        System.out.println(str1.concat(str2));

    }

}

(a) HelloWorld

(b) Hello World

(c) Hello+World

(d) Compilation error

66.When an overridden method is called from within a subclass, it will always refer to

the version of that method defined by the

(a) Super class

(b) Subclass

(c) Compiler will choose randomly

(d) Interpreter will choose randomly

(e) None of the above.

67.Mark the incorrect statement from the following:

(a) Java is a fully object oriented language with strong support for proper software engineering

techniques

(b) In java it is not easy to write C-like so called procedural programs

(c) In java language objects have to be manipulated

(d) In java language error processing is built into the language

(e) Java is not a language for internet programming.

(e) Default constructor.

68. Which of the following is not a component of Java Integrated Development Environment (IDE)?

(a) Net Beans (b) Borland’s Jbuilder

(c) Symantec’s Visual Café 

(d) Microsoft Visual Fox Pro

(e) Microsoft Visual J++

BELOW IS THE CODE FOR THE EMPLOYEE PAYMENT SYSTEM RESULTS WRITTEN TO A FILE

https://github.com/Prodigy-Genes/PAysYstem.git

Leave a Reply

Your email address will not be published. Required fields are marked *