Programming in Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Programming in Java

Multiple choice questions 8. Name the term where if parents object is killed,
child object is also killed.
1. Which is not OOPS concept in Java? (a) Aggregation (b) Composition
(a) Inheritance (b) Encapsulation (c) Encapsulation (d) Association
(c) Polymorphism (d) Compilation 9. What is it called where object has its own lifecycle
2. Identify the type of polymorphism in Java? and child object cannot belong to another parent
object?
(a) Compile time polymorphism
(a) Aggregation (b) Composition
(b) Execution time polymorphism
(c) Encapsulation (d) Association
(c) Multiple polymorphism
10. Combination of inheritance and polymorphism is
(d) Multilevel polymorphism
Method overriding?
3. At what time method overloading is determined?
(a) True (b) false
(a) At run time (b) At compile time
11. Which object is encapsulated inside the System
(c) At coding time (d) At execution time class?
4. Overloading does not occur when? (a) out (b) println
(a) More than one method with same name but (c) Both (a) and (b) (d) None of the above
different method signature and different 12. Employee emp = ___ Employee (); Choose a
number or type of parameters suitable word so that an object of the class
(b) More than one method with same name, same Employee is created.
signature but different number of signature (a) object (b) class
(c) More than one method with same name, same (c) run (d) new
signature, same number of parameters but 13. What is the output for the code:
different type System.out.println (“Hello World”);
(d) More than one method with same name, same (a) Shows error
number of parameters and type but different (b) Hello World
signature
(c) “Hello World”
5. Which concept of Java is used to convert a real (d) None of the above
world objects in terms of class?
14. What element provides a standard interface to
(a) Polymorphism (b) Encapsulation common system resources.
(c) Abstraction (d) Inheritance (a) new (b) API
6. The concept of Java which is achieved by (c) System (d) None of the above
combining methods and attribute into a class 15. SDK is also know as :
(a) Encapsulation (b) Inheritance (a) devkit (b) JVM
(c) Polymorphism (d) Abstraction (c) JDK (d) None of the above
7. An object which has its own lifecycle and there is 16. Feature of Java which is used to dynamically link
no owner is called ? code in a safe and expedient manner:
(a) Aggregation (b) Composition (a) Secure (b) Distributed
(c) Encapsulation (d) Association (c) Dynamic (d) Robust
17. Which additional package is included by JDK: 28. String indexes begin with
(a) java.awt (a) 1 (b) 3
(b) sun.addtools.debug (c) 0 (d) 2
(c) java.util 29. Name the special operator to allocate memory
(d) sun.tools.debug (a) New (b) Old
18. Which is not a feature of Java? (c) ++ (d) –
(a) Portable 30. String and StringBuffer classes are defined in ____
package:
(b) Structured
(a) java.awt (b) java.io
(c) Distributed
(c) java.lang (d) java.util
(d) High Performance
19. Java run-time system that chooses to execute the Fill in the blanks
JAVA Bytecode:
(a) SDK (b) JDK 31. ________Method used to extract a single character
from a String:
(c) JVM (d) None of the above
(a) toCharArray( ) (b) getChars( )
20. Operators that are used to compare two values
and give the results: (c) getBytes( ) (d) charAt( )
(a) Increment and Decrement 32. Human Being and Elephant fall under ______ of
the following relationship:
(b) Logical
(a) Kind-Of (b) Is-A
(c) Comparison
(c) Part-Of (d) Has-A
(d) Arithmetic
33. ________ allows the creation of hierarchical
21. What is the result of the expression: 10+5*8-15/5
classifications?
(a) 3 (b) 47
(a) Interface (b) Inheritance
(c) 7 (d) 21
(c) Package (d) Polymorphism
22. Common programming construct that is based
34. A class member that has been declared as private
upon a sequence of nested if:
will be ___ to its class.
(a) switch (b) nested if
(a) Friendly (b) Public
(c) if-else-if ladder (d) None of the above
(c) Protected (d) Private
23. Identify the Java’s multi-way branch statement.
35. ________ keywords is used to prevent inheritance:
(a) switch (b) nested if
(a) final (b) catch
(c) break (d) if-else-if ladder
(c) extends (d) super
24. Loop that always executes its body at least once,
36. Java supports ___ access specifiers.
even though the condition is not true:
(a) 1 (b) 2
(a) for (b) do-while
(c) 3 (d) 4
(c) while (d) continue
37. ___ is used as a base class to derive specific classes
25. Which statement is used to exit from a loop?
of the same kind.
(a) continue (b) quit
(a) private (b) friend class
(c) break (d) None of the above
(c) abstract class (d) superclass
26. State the method which can be used to set the size
38. Writing the same code in different places, leading
of the buffer:
to unnecessary replication of code is know as:
(a) ensureCapacity( ) (b) length( )
(a) Code extensibility (b) Code redundancy
(c) capacity( ) (d) setLength( )
(c) Code reusability (d) None of the above
27. The method that returns the reversed object on
39. The block which can be nested is:
which it is called is:
(a) catch (b) finally
(a) insert( ) (b) replace( )
(c) try (d) None of the above
(c) delete( ) (d) reverse( )
40. Number of final blocks which are used for an (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
exception-handler. (b) 1-(ii), 2-(i), 3-(iii), 4-(iv)
(a) 1 (b) 2 (c) 1-(i), 2-(ii), 3-(iv), 4-(iii)
(c) 3 (d) 4
(d) 1-(iv), 2-(i), 3-(ii), 4-(iii)
Match the following 44. Match the columns:
Group A Group B
41. Match the columns: 1. Abstract Method (i) Abstract class
Group A Group B 2. We can’t create an (ii) doesn’t have a
1. an object of an (i) Filter streams instance of body
exception class as 3. Constructor can (iii) Alan Kay
a parameter. return a value
2. Data Input Stream (ii) catch 4. OOPs is invented (iv) False
and Data Output by
Stream classes (a) 1-(i), 2-(ii), 3-(iv), 4-(iii)
3. In Unicode (iii) Random Access (b) 1-(ii), 2-(i), 3-(iv), 4-(iii)
character is File
(c) 1-(iv), 2-(i), 3-(ii), 4-(iii)
represented by
(d) 1-(iv), 2-(iii), 3-(ii), 4-(i)
4. To perform I/O (iv) 16 bits
operations 45. Match the columns:

(a) 1-(iii), 2-(ii), 3-(i), 4-(iv) Group A Group B

(b) 1-(iii), 2-(iv), 3-(i), 4-(ii) 1. OOP boost the (i) Exceptions
code reusability
(c) 1-(ii), 2-(i), 3-(iv), 4-(iii)
2. common class (ii) Inheritance
(d) 1-(i), 2-(iv), 3-(iii), 4-(ii)
for exception
42. Match the columns: handling
Group A Group B 3. Java is developed (iii) James Gosling
1. foundation for (i) skip( ) by
the output class 4. one of the (iv) Sun Microsystems
hierarchy inventors of Java
2. Input Stream class (ii) Output Stream (a) 1-(i), 2-(ii), 3-(iii), 4-(iv)
method is
(b) 1-(iii), 2-(ii), 3-(i), 4-(iv)
3. Graphics class is a (iii) start( )
(c) 1-(ii), 2-(i), 3-(iv), 4-(iii)
part of
(d) 1-(i), 2-(ii), 3-(iv), 4-(iii)
4. called immediately (iv) java.awt package
after the init() is Programming based questions
called
(a) 1-(i), 2-(iii), 3-(ii), 4-(iv) 46. Which syntax is used to create an object of Class in
(b) 1-(ii), 2-(i), 3-(iv), 4-(iii) Java?
(c) 1-(ii), 2-(i), 3-(iii), 4-(iv) (a) Classname obj = new() Classname()
(d) 1-(iv), 2-(ii), 3-(i), 4-(iii) (b) Classname obj = new Classname;
43. Match the columns: (c) Classname obj = new Classname();
Group A Group B (d) None of the above
1. abstract data type (i) extends 47. Choose the correct statement
2. inherit a class (ii) Class syntax to create an object of Class in Java:
3. private member of (iii) Access Modifiers Classname obj = new Classname();
a class is accessible How to create an Abstract class?
4. private, public & (iv) only members of (a) Creating at least one member function as a
protected are the same class pure virtual function
(b) Creating at least one member function as a (b) Object can be created only with one parameter
virtual function (c) 
Object can be created with more than one
(c) 
Declaring as Abstract class using virtual parameter
keyword (d) Objects can be create only without parameter
(d) 
Declaring as Abstract class using static 51. Choose correct option for the code .
keyword class A{ static int c=0; public: A(){ c++; } };
48. Pick the correct syntax for class ? (a) 
Constructor will make c=1 for each object
class A created
{ (b) 
Constructor will make c=0 for each object
int a,b; created
public : void disp(); (c) 
Constructor will keep number of objects
} created
(a) void disp::A(){ } (d) 
Constructor will just initialize c=0 then
(b) void A::disp(){ } increment by 1
(c) void A:disp() { cout<<a<<b ; } 52. Identify the data member in following code which
will be used when an object is created?
(d) void disp:A(){ cout<<a<<b; }
Class A
49. Select the correct statement for the given code?
{
class student
int x; int y; int z;
{
public : A()
private: student()
{
{
y=100; x=100*y;
}
}
public : student( int x)
};
{
(a) x will be used (b) y will be used
marks =x;
(c) z will be used (d) All will be used
}
53. Select the member considered most secure in the
};
code ?
(a) The object can never be created
class A()
(b) The object can be created without parameters
{
(c) Only the object with only 1 parameter can be
int a;
created
private : int b;
(d) Only the object with some parameters can be
created protected : int c;
50. The true statement for the code is: public : int d;
class A };
{ (a) a (b) b
private : int marks; char name[20]; (c) c (d) d
public : 54. Choose the correct option for the given code?
A(int x=100) class A
{ {
marks=x; private : A()
} {
}; }
(a) Objects can be created with one parameter or public : A(int x)
without parameter {
} (ii) System.out.println
}; (iii) public
A a; (iv) static void main()
A b(100); 56. Regarding the program code given below, answer
(a) Program will give compile time error the questions that follow:
(b) Program will run fine // filename Test.java
(c) Program will give runtime error class Test {
(d) Program will give logical error public static void main(String[] args) {
55. About the program code given below, answer the for(int i = 0; 1; i++) {
questions that follow: System.out.println(“Hello”);
public class AddTwoNumbers { break;
public static void main(String[] args) { }
int num1 = 5, num2 = 15, sum; }
sum = num1 + num2; }
  System.out.println(“Sum of these (a) What will be the output?
numbers: “+sum); (i) Compiler Error
} (ii) syntax error
} (iii) run time error
(a) What will be the output? (iv) logic error
(i) Sum of these numbers: 20 (b) What is the name of the class?
(ii) Sum of these numbers: 40 (i) System.out.println
(iii) Sum of these numbers: 60 (ii) static void main()
(v) Sum of these numbers: 80 (iii) public
(b) What is the name of the class? (iv) Test()
(i) AddTwoNumbers

Answers

Multiple choice questions constructor and also when same signature but
different number of parameters and/or parameter
1. (d) Compilation type.
Explanation: The OOPs Concepts in Java are 5. (c) Abstraction
abstraction, encapsulation, inheritance, and Explanation: abstract is a non-access modifier
polymorphism. in java applicable for classes, methods but
2. (a) Compile time polymorphism not variables. It is used to achieve abstraction
Explanation: If you overload a static method which is one of the pillar of Object Oriented
in Java, it is the example of compile time Programming(OOP).
polymorphism. 6. (a) Encapsulation
3. (b) At compile time Explanation: Encapsulation in Java is a mechanism
Explanation: Overloading is determined at of wrapping the data and code acting on the data
compile time. Hence, it is also known as compile together as a single unit.
time polymorphism.
7. (d) Association
4. (d) More than one method with same name, same
Explanation: Association in Java is a connection
number of parameters and type but different
or relation between two separate classes that are
signature
set up through their objects.
Explanation: Overloading occurs when more
8. (b) Composition
than one method with same name but different
Explanation: A composition in Java between two within a loop, the loop iterations stop there, and
objects associated with each other exists when control returns from the loop immediately to the
there is a strong relationship between one class first statement after the loop.
and another. Other classes cannot exist without 26. (a) ensureCapacity( )
the owner or parent class. Explanation: The ensureCapacity() method of
9. (a) Aggregation java.util.ArrayList class increases the capacity of
Explanation: Aggregation in Java is a relationship this ArrayList instance, if necessary, to ensure
between two classes .The aggregate class contains that it can hold at least the number of elements
a reference to another class and is said to have specified by the minimum capacity argument.
ownership of that class. Syntax:
10. (a) True public void ensureCapacity(int minCapacity)
11. (a) out 27. (d) reverse( )
12. (d) new Explanation: The Java. lang. StringBuffer. reverse()
is an inbuilt method which is used to reverse the
13. (b) Hello World
characters in the StringBuffer. The method causes
14. (c) System this character sequence to be replaced by the
15. (a) devkit reverse of the sequence.
16. (c) Dynamic 28. (c) 0
17. (d) sun.tools.debug Explanation: Strings are ordered sequences of
18. (b) Structured character data, 00:15 and the individual characters
Explanation: Java is fast, Portable, Distributed, of a string can be accessed directly using that
numerical index. String indexing in Java is zero-
High Performance ,secure, and reliable
based, so the very first character in the string
19. (c) JVM
would have an index of 0 , 00:30 and the next
20. (c) Comparison would be 1 , and so on.
21. (b) 47 29. (a) New
22. (c) if-else-if ladder Explanation: You can allocate memory at run time
Explanation: Java if-else-if ladder is used to within the heap for the variable of a given type
decide among multiple options. The if statements using a special operator in JAVA which returns
are executed from the top down. the address of the space allocated. This operator
23. (a) switch is called new operator.

Explanation: The switch statement is a multi- 30. (c) java.lang


way branch statement. It provides an easy way Explanation: java.lang provides classes that
to dispatch execution to different parts of code are fundamental to the design of the Java
based on the value of the expression. Basically, programming language. The most important
the expression can be byte, short, char, and int classes are object , which is the root of the class
hierarchy, and class , instances of which represent
primitive data types.
classes at run time.
24. (b) do-while
Explanation: do-while loop is used to iterate a Fill in the blanks
part of the program repeatedly, until the specified
condition is true. If the number of iteration is not 31. (d) charAt( )
fixed and you must have to execute the loop at 32. (a) Kind-Of
least once, it is recommended to use a do-while 33. (b) Inheritance
loop. 34. (d) Private
25. (c) break 35. (a) final
Explanation: Break Statement is a loop control 36. (c) 3
statement that is used to terminate the loop. As 37. (c) abstract class
soon as the break statement is encountered from 38. (b) Code redundancy
39. (c) try 49. (c) Only the object with only 1 parameter can be
40. (a) 1 created
50. (a) Objects can be created with one parameter or
Match the following without parameter
51. (c) 
Constructor will keep number of objects
41. (c) 1-(ii), 2-(i), 3-(iv), 4-(iii)
created
42. (b) 1-(ii), 2-(i), 3-(iv), 4-(iii)
52. (c) z will be used
43. (a) 1-(ii), 2-(i), 3-(iv), 4-(iii)
53. (b) b
44. (c) 1-(iv), 2-(i), 3-(ii), 4-(iii)
54. (a) Program will give compile time error
45. (d) 1-(i), 2-(ii), 3-(iv), 4-(iii)
55. (a) (i) Sum of these numbers: 20
Programming based questions (b) (i) AddTwoNumbers
56. (a) (i) Compiler Error
46. (c) Classname obj = new Classname();
Explanation: There is a mistake in condition
47. (a) Creating at least one member function as a check expression of for loop. instead of using 1
pure virtual function use valid.
48. (b) void A::disp(){ } (b) (iv) Test()

You might also like