java_mcq-1
java_mcq-1
2. A feature of Java programming that lets you create classes that are derived from other classes is called:
A) Polymorphism C) Inheritance B) Encapsulation D) Abstraction
3. A class inheriting from other class and adding its own methods to it is called:
A) Abstract class B) Super class C) Sub class D) Main class
6. Hiding the implementation details and showing only functionality to the user is called:
A) Abstraction B) Inheritance C) Encapsulation D) Polymorphism
A. Top-down approach B. Bottom-up approach C. Top and Bottom Approach D. None of the above
13. What represents an entity in the real-world with its identity and behaviour.
A. method B. A class C. A procedure D. An object
15. An object belonging to a particular class is known as a/an ------ of that class.
A. Instance B. Alias C. Interface D. Member
18. Which option is the technique of binding both data and methods together to keep them safe from unauthorised access
and misuse?
A. Abstraction B. Encapsulation C. Polymorphism D. Inheritance
19. The ability of a method or object to take on multiple forms is known as:
A. Polymorphism B. Encapsulation C. Abstraction D. Inheritance
20. Objects that share the same attributes and behaviour are grouped together into a/an:
A. Alias B. Interface C. Instance D. Class
21. A feature that enables one class to acquire the properties of another class:
A. Encapsulation B. Abstraction C. Inheritance D. Polymorphism
23. Which option servers as a template to create similar objects that share common characteristics and behaviour?
A. An attribute B. A method C. A class D. A procedure
24. Which option refers to the act of representing essential features without including the background details?
A. Abstraction B. Encapsulation C. Inheritance D. Polymorphism
25. The values of an object’s -------- represent the state of the object.
A. attributes B. methods C. classes D. procedures
14. A computer program that translates a high-level language program into machine code is:
16. A computer program that translates an assembly language program into machine code is:
2.Which character set would you use if you have characters from Hindi, Japanese and German to display?
A)ASCII Character Set B)Unicorn Character Set
B)Extended ASCII Character Set D)Unicode Character Set
11. Which of the following is the size of short data type in Java?
A) 8 bits B) 24 bits C) 16 bits D) 32 bits
12. Which of the following is the size of char data type in Java?
A) 8 bits B) 24 bits C) 16 bits D) 64 bits
13. Given statement int x = ‘Z’; What is the value of x? ( As ASCII value for A = 65, B = 66,…..Z =90 )
( ASCII value for a = 97, b = 98 ,………z=122 )
A) 90 B) 65 C) 91 D) Invalid assignment
15. The keyword that converts a variable with its value into a constant is:
A) const B) constant C) fixed D) final
20. Arrange the following primitive data types in the ascending order of size:
A) byte, char, int, double B) char, byte, int, double
C) byte, char, double, int D) char, byte, int, double
4. Statement i = i -1 is equivalent to
A)i-- B) - -i C) i -= 1 D)All of the above
6. Given num1 = 0, num2 = 1 the statement (num1 < num2) && (num1 > num2) evaluates to
A)true B)false C)1 D)0
7.Given num1 = 0, num2 = 1 the statement (num1 < num2) || (num1 > num2) evaluates to
A)true B)false C)1 D)0
13. What is the value of y after evaluating the expression given below?
y += ++y + y-- + --y; when int y = 8.
A) 32 B) 33 C) 34 D) 35
16. What will be the result stored in X after evaluating the following expression? int X = 4;
X += (X++) + (++X) + X;
A) 19 B) 20 C) 21 D) 22
3. Name the type of error that occurs for the following statement:
int a;b;c;
A) Syntax error B)Runtime error C)Logical error D)No error
4.Pick the statement that will include all the classes present in java.util package into your program:
A) include java.util B) java.util C)package java.util D) import java.util.*
5. Name the type of error that occurs for the following scenario:
Division by a variable that contains a value zero.
A)Syntax error B)Runtime error C)Logical error D)No error
6. Name the type of error that occurs for the following scenario:
Multiplication operator used when the operation should be division.
A) Syntax error B) Runtime error C) Logical error D) No error
7. Name the type of error that occurs for the following scenario:
Missing semicolon at the end of a statement.
A) Syntax error B) Runtime error C) Logical error D) No error
10. Which of the following statement we must write to import just the Scanner class from the java.util package
A) import java.util.*
B) import package java.util.Scanner
C) include java.util.Scanner
D) import java.util.Scanner
12. Name the method of Scanner class that is used to input an integer data from the standard input device
A) NextInt() B) nextInteger() C) nextNumber() D) nextInt()
14. Name the input source that a Scanner object uses to accept data from the standard input device
A) System.in B) System.input C) Scanner.in D) Java.input
16. Pick the invalid method that is not in the Scanner class:
A) nextInt() B) next() C) nextNumber() D) nextLong()
20. Name the method of Scanner class that checks if the Scanner has another token in its input:
A) hasNext() B) nextToken() C) getNext() D) hasNextToken()
Math.ceil(-5.4) + Math.ceil(-4.5)
A)-9.0 B)-10.0 C)9.0 D)10.0
15. if ((a < b) && (a < c)), then which of the following statement is true?
A) a is the smallest number B) b is the smallest number
C) c is the smallest number D) b is the largest number
Iterative Constructs
I. Objective Type Questions:
int counter = 1;
do
{
System.out.println(counter);
}
while (++counter < 5);
A)4 B)5 C)6 D)Infinite time
5. What will be the final value of x and y at the end of code snippet?
int x = 2, y = 10;
for (int i = 1; i < 5; i++)
x++; y++;
A) x = 6, y = 14 B) x = 6, y = 11 C) x = 5, y = 11 D) x = 5, y = 14
8. How many times the following code snipped will display “Loop”?
for (int i = 1; i <= 3; i++)
for (int j = 1; j <=3; j++);
System.out.println("Loop");
A) 1 B) 3 C) 9 D) 10
9. Which of the following statement is correct?
A) A for loop always executes once B) A while loop always executes once
C) A do-while loop always executes once D) All loops always execute once
13. Which of the following for loops will cause the body of the loop to be executed 5 times?
A) for (int i = 5; i > 1; i-- ) B) for (int i = 1; i < 5; i++ )
C) for (int i = 0; i < 5; i++ ) D) for (int i = 0; i <= 5; i++ )
7. A class contains:
A) member variables only B) member methods only C) void methods only
D) member variables and member methods
8. When two variables containing composite data are compared, they are compared:
A) By value only B) By reference only
C) Both by value and reference D) Cannot be compared
10. Choose the method that converts a string to a primitive integer data type:
A) Integer.parse() B) Integer.toString() C) Parse.IntegerNumber() D) Integer.parseInt()
A) Only 1) is true B) Only 2) is true C) Both 1) and 2) are true D) 1) is true but 2) is false
13. Choose the method that converts a string to a primitive float data type:
A) Float.parseFloat() B) Float.convert()
C) Parse.floatNumber() D) Float.parse()
15. The keyword that distinguishes between instance variable and class variable is:
A) static B) final C) private D) protected
8. Choose the correct option that defines a prototype of the method “sum” that takes an integer variable x
as its argument and returns a value of float data type.
A) int sum(float x) B) void float sum(int x) C) float sum(int x) D) void sum(int x)
10. Choose the correct option that defines a prototype of the method “check” which receives a character ch and
an integer n and returns true or false value.
A) boolean check(char ch, int n) B) void boolean check(char ch, int n)
C) boolean check(int ch, int n) D) boolean check(char ch, char n)
11. Choose the correct option that defines a prototype of the method PosChar that takes a string argument and
a character argument and returns an integer value.
A) int PosChar(String stg, character ch) B) int PosChar(String stg, char ch)
C) void int PosChar(String stg, char ch)
D) int void PosChar(String stg, char ch)
12. Choose the correct option that defines a prototype of the method “search” which receives a sentence sentnc and a word
wrd and returns 1 or 0.
A) int search(String sentnc, String wrd) B) int search(Sentence sentnc, Word wrd)
C) void search(String sentnc, String wrd) D) void int search(String sentnc, String wrd)
14. A method that doesn’t return a value has a return type of:
A) null B) void C) int D) new
15. The parameters that appear in the method call statement are:
A) call parameters B) void parameters C) actual parameters D) formal parameters
Constructors
I.Objective Type Questions:
1.A class having multiple definitions of constructors is termed as Constructor ----
A)Abstraction B)Overlaying C)Overloading D)Multiclass
8. It is allowed to have a member method with a name same as that of the class when:
A) it is a constructor B) the class is a super class C) the class is a sub class D) the class has void methods
9. Given a class named Rectangle, which is/are valid constructor names of the class:
A) public Rectangle () B) public Rectangle (int length)
C) public Rectangle (int length, int width) D) All of the above are valid constructors
10. Given a class named Student, which is a valid constructor name of the class:
A) public Student() B) public void Student() C) public student() D) public void student()
12. Given a class named Cuboid, which statement invokes the default constructor:
A) Cuboid cuboid = new Cuboid(); B) Cuboid cuboid = new Cuboid(10);
C) Cuboid cuboid = new Cuboid(10, 20); D) Cuboid cuboid = new Cuboid(10, 20, 30);
13. Name the keyword that is used to resolve the conflict between parameter and instance variables:
A) reference B) new C) this D) void
Ans. this keyword is used to represent current object on which a function is called.
There may be more than one objects of a single class and each object may invoke the
same function of the class. For a function to differentiate its working for both objects, this
keyword is required. This refers to the current object that invoked the function.
a) Name the keyword is used to resolve the conflict between method parameter and instance variables/fields.
b) Ans : Keyword this is used.
class This_example
{
int length, breadth;
This_example ()
{
length = 100;
breadth = 200; length breadth
}
void rectangle(int l, int b) 100 200
{
this.length=l;
this.breadth=b;
} this.length this.breadth
void display( )
{ 5 10
System.out.println(length);
System.out.println(breadth); Output
}
void display( ) 100
{ 200
System.out.println(this.length); 5
System.out.println(this.breadth);
} 10
public static void main ( )
{
This_example ob = new Thisexample_ex ( );
ob.display();
This_example ob1 = new This_example ( );
ob1.rectangle(5, 10);
ob1.display1( );
}
}
WRITE THE OUTPUT OF THE PROGRAM.
import java.util.*;
class This_exam
{
int length, breadth;
This_exam()
{
length = 100;
breadth = 200;
}
void rectangle(int l, int b)
{
this.length=l;
this.breadth=b;
}
void display( )
{
System.out.println(length);
System.out.println(breadth);
System.out.println(length * breadth);
}
void display1( )
{
System.out.println(this.length);
System.out.println(this.breadth);
System.out.println(this.length * this.breadth);
System.out.println("this.length " + this.length);
System.out.println("breadth " + breadth);
System.out.println("this.length * this.length " + this.length * length);
System.out.println("\n\nlength * this.breadth " + length * this.breadth);
}
public static void main (String args[])
{
This_exam ob = new This_exam( );
ob.display();
This_exam ob1 = new This_exam( );
ob1.rectangle(5, 10);
ob1.display1( );
}
}
Take print out and study. Any doubts u can call me and clarify .