Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views43 pages

Cs 103 Assignment

The document contains a series of multiple-choice questions related to Java programming concepts, specifically focusing on classes, methods, constructors, and access modifiers. Each question is followed by the correct answer, highlighting key programming principles such as encapsulation, method overloading, and the characteristics of variables. The content serves as a study guide for understanding fundamental Java programming topics.

Uploaded by

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

Cs 103 Assignment

The document contains a series of multiple-choice questions related to Java programming concepts, specifically focusing on classes, methods, constructors, and access modifiers. Each question is followed by the correct answer, highlighting key programming principles such as encapsulation, method overloading, and the characteristics of variables. The content serves as a study guide for understanding fundamental Java programming topics.

Uploaded by

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

Cs 103 Assignment

1) …… is a blueprint from which individual objects are created.

● Method

● Constructor

● Class

● Function

* The correct answer is Class.

2) ……. can contain any of the following variable types: Local variables, Instance variables,
Class variables.

● Object

● Class

● Object, Class

● Constructor

* The correct answer is Class.

3)........... are variables declared within a class, outside any method, with the static
keyword.

● Local variables

● Instance Variables

● Class Variables

● All of them
* The correct answer is Class Variables.

4) ........ are Variables defined inside methods, constructors or blocks.

● Local Variables

● Instance Variables

● Class Variables

● All of them

* The correct answer is Local Variables.

5) Every class has a ..................... if we do not explicitly write one for a class, the java
compiler builds a default one for that class.

● Object

● Interface

● Method

● Constructor

* The correct answer is Constructor.

6) Each time a new object is created, at least one .............. will be invoked.

● Object

● Interface

● Method

● Constructor

* The correct answer is Constructor.


7) The main rule of constructors is that they should have the same name as the main
...........

● Method

● Object

● Class

● Interface

* The correct answer is Class.

8) A class can have ................ constructors.

● Only one

● Two

● Infinite

● All of them

* The correct answer is All of them.

9) The .......... Keyword is used to created objects from class.

● Super

● Static

● New

● Private

* The correct answer is New.


10) A ........ is a collection of statements that are grouped together to perform an operation.

● Method

● Function

● Constructor

● All of them

* The correct answer is All of them.

11) The System.out.println() method, has .... return value type.

● Int

● Double

● Void

● All of them

* The correct answer is Void.

12) In the method header, which part defines the access type of the method.

● Return type

● Parameter list

● Method body

● Modifier

* The correct answer is Modifier.


13) The method returning ...... is considered as call to a statement.

● Int

● Double

● Void

● All of them

* The correct answer is Void.

14) The ....... Keyword allows us to create method which do not return a value. Super New
Void Print.

● Super

● New

● Void

● Print

* The correct answer is Void.

15) A method that calls itself is called ........ method.

● Overload

● Overriding

● Recursive

● None of these

* The correct answer is Recursive.


16) A single program of OOPS contains ...... classes.

● Only 1

● Only 999

● Only 1000

● Any number

* The correct answer is Any number.

17) Which of these operators is used to allocate memory to array variable in java?

● Super

● Array

● New

● Memory

* The correct answer is New.

18) Which of these is incorrect Statement?

● It is necessary to use new operator to initialize an array.

● Array can be initialized using comma separated expressions surrounded by curly braces.

● Array can be initialized when they are declared.

● None of the mentioned

* The correct answer is It is necessary to use new operator to initialize an array.


19) What will be the output of the following java code?

●02468

●13579

●0123456789

● 1 2 3 4 5 6 7 8 9 10

* The correct answer is 0 2 4 6 8

20) What is the process of defining more than one method in a class differentiated by
method signature?

● Method overriding

● Method overloading

● Method doubling

● None of these
* The correct answer is Method overloading.

21) A specific element in an array is accessed by its .......

● Location

● Name

● Index

● Value

* The correct answer is Index.

22) ........ have no explicit return type. Method Function Constructor None of these.

● Method

● Function

●Constructor

● None of these

* The correct answer is Constructor.

23) ....... is used to give initial values to the instance variables defined by the class, or to
procedures required to create object.

● Method

● Function

● Constructors

● None of these

* The correct answer is Constructors.


24) All classes have............, whether you define one or not, because java automatically
provides a default one that initializes all member variables to zero.

● Variables

● Objects

● Constructors

● None of these

* The correct answer is Constructors.

25) ....... does not accept any parameters instead, the instance variables of a method will
be initialized with a fixed values for all objects.

● No argument constructors

● All types of constructors

● Parameterized constructors

● All of them

* The correct answer is No argument constructors.

26) A variable or method declared without any access control modifier is a considered to
have ..........

● Public access modifier

● Private access modifier

● Default access modifier

● None of these

* The correct answer is Default access modifier.


27) A variable or method declared with …...... modifier is available to any other class in the
same package.

● Public access modifier

● Private access modifier

● Default access modifier

● None of the above

* The correct answer is Default access modifier.

28) Method, variables, and constructors that are declared ...... can only be accessed within
the declared class itself.

● Public access modifier

● Private access modifier

● Default access modifier

● None of these

* The correct answer is Private access modifier.

29) Using the ........ modifier is used by an object to encapsulate itself and hide data from
the outside world.

● Public access modifier

● Private access modifier

● Default access modifier

● None of these

* The correct answer is Private access modifier.


30) Variables that are declared …...... can be accessed outside the class, if public getter
methods are present in the class.

● Public access modifier

● Private access modifier

● Default access modifier

● None of these

* The correct answer is Private access modifier.

31) A class, method, constructor, interface, etc. declared ........... can be accessed from
any other class.

● Public access modifier

● Private access modifier

● Default access modifier

● None of these

* The correct answer is Public access modifier.

32) When there may be two or more possible matches for an invocation of a method, but
the compiler cannot determine the most specific match this is called ............

● Recursion

● Overriding

● Overloading

● Ambiguous calling

* The correct answer is Ambiguous calling.


33) When an array is passed to a method, what does the method receive?

● The reference of the array

● A copy of the array

● Length of the array

● Copy of first element

* The correct answer is The reference of the array.

34) Identify the correct restriction on static methods.

1. They must access only static data.

2. They can only call other static methods.

3. They cannot refer to this or super.

●1&2

●2&3

● only 3

● 1, 2 and 3

* The correct answer is 1, 2 and 3.

35) Identify the keyword among the following that makes a variable belong to a class, rather
than being defined for each instance of the class.

● final

● Static

● Volatile

● Abstract
* The correct answer is Static.

36) Identify what can directly access and change the value of the variable res.

● Any class

● Only Solution class

● Any class that extends

● None of these

* The correct answer is Only Solution class.

37) Where does the System stored parameters and local variables whenever a method is
invoked?

● Heap

● Stack

● Array

● Tree

* The correct answer is Stack.

38) Identify the modifier which cannot be used for a constructor.


● Public

● Protected

● Private

● Static

* The correct answer is Static.

39) What are the variables declared in a class for the use of all methods of the class called?

● Object

● Instance variables

● Reference

● Variables

● None of these

* The correct answer is Instance variables.

40) Employee emp = ...... Employee(); pick a suitable word from the list so that an object of
the class Employee is created.

● new

● odd

● ++

●–

The correct answer is new.

41) Java supports ...... access specifiers.


●1

●2

●3

●4

* The correct answer is 4 (public, protected, default and private).

42) Encapsulation implementation involves the use of the set and get keywords.

● True

● False

* The correct answer is False.

43) Keyword ………. specifies that a variable is not modifiable.

● final

● Constant

● Static

● None of these

* The correct answer is final.

44) A(n) variable ........ represents class wide information that is shared by all the objects of
the class.
● final

● Instance

● Static

● None of these

* The correct answer is Static.

45) Set methods are called …....... because they typically change.

● Mutator

● Accessor

● Instance

● Static

* The correct answer is Mutator.

46) A Static method must be also public.

● True

● False

* The correct answer is True.

47) A Static method must be also public.

● True

● False

* The correct answer is True.


48) Method overloading implies writing another method with the same name and a
different return type.

● True

● False

* The correct answer is False.

49) A constructor is automatically called when an object is instantiated.

● True

● False

* The correct answer is True.

50) Get methods are commonly called ……….

● Accessor method

● Static method

● final method

● Mutator method

* The correct answer is Accessor method.

51) A constructor is a method that never returns anything, in fact it has void return type.

● True
● False

* The correct answer is False.

52) A constructor is called the "default constructor". if it has no parameters.

● True

● False

* The correct answer is False.

53) An instance variable belongs to the class in which it is declared.

● True

● False

* The correct answer is False.

54) An array is an object and can therefore be declared, instantiated and initialized.

● True

● False

* The correct answer is True.

55) The length method returns the number of elements of an array.

● True
● False

* The correct answer is False.

56) An array is not resizable.

● True

● False

* The correct answer is True.

57) What is the number of parameters that a default constructor requires?

●0

●1

●2

●3

* The correct answer is 0.

58) ......... is the combination of the method name and the parameter list.

● Actual parameters

● Formal parameters

● Access modifier

● Method Signature

* The correct answer is Method Signature.


59) The variables defined in the method header are known as ......

● Actual parameter

● Formal Parameter

● Access modifier

● Method signature

* The correct answer is Formal Parameter.

60) When a method is invoked, you pass a value to the parameter. this value is referred to
as .........

● Actual parameters

● Formal parameters

● Access modifier

● Method signature

* The correct answer is Actual Parameters.

61) int result = sum(6,9); The return Value Type of this method is ….....

● int

● result

● void

● sum

* The correct answer is int.

62) Method can be invoked from other classes by ........


● method name

● class name

● class.method

● method.class

* The correct answer is class.method.

63) In pass by value the variable is not affected, regardless of the changes made to the
parameter inside the method.

● True

● False

* The correct answer is True.

64) The scope of a local variable starts from its …...... and continues to the end of the ........
that contains the variable.

● name, method

● Data type, class

● Declaration, block

● Block, Class

* The correct answer is Declaration, block.

65) You can declare a local variable with the same name multiple times in ........

● The same statement


● The same method

● Non nesting blocks

● Nested blocks

* The correct answer is Non nested blocks.

66) The array elements are accessed through the .......

● Size

● Index

● Length

● Type

* The correct answer is Index.

67) The array indices are 0-based, it starts from 0 to ........

● arrayRefvar.length

● arrayRefVar.length-1

● arrayRefVar.size

● arrayRefVar.size-1

* The correct answer is arrayRefVar.length-1.

68) For copying an array list1 to list2, it copies the......from list1 to list2.

● index
● elements value

● length

● reference value

* The correct answer is reference value.

69) When passing an array to a method, the ...... is passed to the method.

● index

● elements value

● length

● reference value

* The correct answer is reference value.

70) When an array is created, its elements are assigned ….... the default value of numeric
data type.

● null

● index

● Variable

● Zero

* The correct answer is Zero.

71) When an array is created, its elements are assigned ........ the default value of Boolean
data type.

● null
● True

● False

● Zero

* The correct answer is False.

72) Once an array is created, its size is .......

● null

● variable

● fixed

● zero

* The correct answer is fixed.

73) mylist[0] reference the …...... element in the array.

● null

● first

● last

● zero

* The correct answer is first.

74) The sate of an object consists of a .........

● set of methods

● set of data fields


● Identity

● None of these

* The correct answer is set of data fields.

75) The set of data fields of an objects are also called ........

● Identity

● Properties

● Behavior

● Methods

* The correct answer is Properties.

76) The ....... of an object is defined by a set of methods.

● Identity

● Properties

● Behavior

● None of these

* The correct answer is Behavior.

77) We can create many objects of a class. Creating an object is referred to as ….....

● Instantiation

● Encapsulation
● Inheritance

● Polymorphism

* The correct answer is Instantiation.

78) When you put the two classes into one file, how many classes in the file can be a public
class.

● only one

● The two classes

● No one

● Any number of classes

* The correct answer is Only one.

79) When you compile one file with 2 classes, how many .class files will be generated?

● Only one

● Two files

● No compiled files

● Any number of files

* The correct answer is Two files.

80) A class may be defined without constructors.

● True

● False
* The correct answer is True.

81) A no-arg constructor with an empty body is implicitly declared in the class when .........

● An object is generated

● A class defined without constructors

● A parametrized constructor is declared

● All of them

* The correct answer is A class defined without constructors.

82) …….. is provided automatically only if no constructors are explicitly defined in the class.

● Default constructor

● Parametrized constructor

● Explicit constructor

● All of them

* The correct answer is Default constructor.

83) What happens when declaring data fields in a class without assigning any values?

● Compilation error

● Default values are assigned

● Variables are not initialized

● None of these

* The correct answer is Default values are assigned.


84) The ...... methods are used to read and modify private properties.

● Setters only

● Getters only

● Both setters and getters

● No methods

* The correct answer is Both setters and getters.

85) The ...... modifier restricts access to within a class.

● Public

● Private

● Default

● All of them

* The correct answer is Private.

86) The ....... modifier restricts access to within a package.

● Public

● Private

● Default

● Protected

* The correct answer is Default.


87) The …..... modifier enables unrestricted access.

● Public

● Private

● Default

● All of them

* The correct answer is Public.

88) ..... is a blue print from which individual objects are created.

● Method

● Constructor

● Class

● Interface

* The correct answer is Class.

89) When is the finalize () method called?

● Before garbage collection

● Before an object goes out of scope

● Before a variable goes out of scope

● None of these

* The correct answer is Before garbage collection.


90) The following method is correctly defined:Public int method () {System.out.println
("Ciao");}.

● True

● False

* The correct answer is False.

91) The following constructor is correctly defined:Public class Computer {Public Computer
(int a) { } }.

● True

● False

* The correct answer is False.

92) Which statement is a true syntax?

● double [ ] my list;

● double mylist [ ];

● mylist = new double [10];

● All of them

* The correct answer is All of them.


93) Which line in the following code represents a constructor:

●1

●3

●5

● 3, 5

● All of them

* The correct answer is 3, 5.

94) According to the program below the compiler will create a default constructor:
● True

● False

* The correct answer is False.

95) Identify the modifier for data filed in line 4 in the following program:

● Public

● Default

● Protected

● Private

* The correct answer is Private.


96) In the following program Counter data field that declared in line 5 is not shared by all
the object of the class:

● True

● False

* The correct answer is False.

97) According to the following program, what is the type of error in line 6:
● Runtime error

● Syntax error

● Logic error

● No error

* The correct answer is Syntax error.

98) What is the explicit return type of constructor?

● No return value

● A class object

● Void

● None of these

* The correct answer is No return value.


99) According to the following program, Is the return type for the method in line 10 is
declared correctly?

● Correct

● Should be String

● Should be student

● According to the object

* The correct answer is Should be string.


100) According to the following program, what is the modifier for the method declared in
line 12:

● Public

● Default

● Protected

● Private

* The correct answer is Default.


101) According to the visibility modifier for the method (setGpa) in line 14 the return
statement in line 15 will cause a syntax error:

● True

● False

* The correct answer is False.

102) According to the following code, methods define in line 17 & 18 are....

● Accessor method

● Mutator method
* The correct answer is Accessor method.

103) According to the following code, method defines in line 14 is....

● Accessor method

● Mutator method

* The correct answer is Mutator method.

104) The purpose of counter data field is counter the number of objects instantiated

● True

● False
* The correct answer is True.

105) What is the data type of variable obj that declared in line 23?

● String

● Student

● Complex

● None of these

* The correct answer is Student.


106) What argument should be passed to constructor called in line 23?

● No Argument

● String only

● String & int

● int & String

* The correct answer is String & int.

107) According to the default data type for the floating-point data in java, is line 24, correct?

● True

● False
* The correct answer is False.

108) What is the visibility modifier for the data field name?

● Public

● Protected

● Default

● Private

* The correct answer is Private.


109) What causing an error in line 25?

● Assign int to Sting

● Undeclared object

● Access private

● None of these

* The correct answer is Access Private.

110) Is the code in line 27, correct:

● True

● False
* The correct answer is False.

111) Method getCounter () should be called by:

● Class identifier

● Object identifier

● Package identifier

● None of these

* The correct answer is Class identifier.

You might also like