Section - A
Question 1
Choose the correct answers to the questions from the given options.
Name the feature of java depicted in the above picture.- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Answer: b. Inheritance- The expression which uses `>=` operator is known as:
- relational
- logical
- arithmetic
- assignment
Answer: a. relational - Ternary operator is a:
- logical operator
- arithmetic operator
- relational operator
- conditional operator
Answer: d. conditional operator - When primitive data type is converted to a corresponding object of its class, it is called:
- Boxing
- Unboxing
- explicit type conversion
- implicit type conversion
Answer: a. Boxing - The number of bytes occupied by a character array of 10 elements
- 20 bytes
- 60 bytes
- 40 bytes
- 120 bytes
Answer: a. 20 bytes - The method of Scanner class used to accept a double value is:
- nextInt()
- nextDouble()
- next()
- nextInteger()
Answer: b. nextDouble() - Among the following which is a keyword
- every
- all
- case
- each
Answer: c. case - The output of Math.round(6.6) + Math.ceil(3.4) is:
- 9.0
- 11.0
- 10.0
- 11
Answer: b. 11.0 - Name the type of error, if any in the following statement: System.out.print("HELLO")
- logical
- no error
- runtime
- syntax
Answer: b. no error - Java statement to access the 5th element of an array is
- X[4]
- X[5]
- X[3]
- X[0]
Answer: a. X[4] - The output of “Remarkable”.substring(6) is:
- mark
- emark
- marka
- able
Answer: d. able - Which of the following is the wrapper class for the data type char?
- String
- Char
- Character
- Float
Answer: c. Character - Name the package that contains wrapper classes:
- java.lang
- java.util
- java.io
- java.awt
Answer: a. java.lang - Constructor overloading follows which principle of Object Oriented programming?
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
Answer: b. Polymorphism - Which of the following is a valid Integer constant:
1. 4
2. 4.0
3. 4.3f
4. "four"- Only 1
- 1. and 3.
- 2. and 4.
- 1. and 2.
Answer: a. Only 1 - The method compareTo() returns __________ when two strings are equal and in lowercase:
- true
- 0
- 1
- false
Answer: b. 0 - Assertion(A): In java statements written in lower case letter or upper case letter are treated as the same
Reason(R): Java is a case sensitive language.- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
- Assertion (A) is true and Reason (R) is false
- Assertion (A) is false and Reason (R) is true
Answer: d. Assertion (A) is false and Reason (R) is true - Read the following text, and choose the correct answer:
A class encapsulate Data Members that contains the information necessary to represent the class and Member methods that perform operations on the data member.
What does a class encapsulate?- Information and operation
- Data members and Member methods
- Data members and information
- Member methods and operation
Answer: b. Data members and Member methods - Assertion(A): call by value is known as pure method
Reason(R): The original value of variable does not change as operation is performed on copied values.- Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
- Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
- Assertion (A) is true and Reason (R) is false
- Assertion (A) is false and Reason (R) is true
Answer: a. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A) - What Will be the output for:
System.out.print(Character.toLowerCase('1'));- 0
- 1
- A
- true
Answer: b. 1
Question 2
Write the Java expression for (p + q)2
Answer
(p + q) * (p + q)valuate the expression when the value of
x = 2:x = x ++ + ++ x + xAnswer
10
Explanation
The following expression/statement can be written as:
x = (x ++) + (++ x) + xNow,
x++which is post-increment will return the value of x, 2, and then increment it to 3. So now we havex = 2 + (++x) + x // now x is 3Next,
++xwhich is pre-increment will return the value of x after incrementing, so++x = 3 + 1 = 4. Now we have:x = 2 + 4 + x or x = 6 + x // x is now 4Hence,
x = 6 + 4which is 10.The following code segment should print “You can go out” if you have done your homework (dh) and cleaned your room(cr). However, the code has errors. Fix the code so that it compiles and runs correctly.
boolean dh = True; boolean cr= true; if (dh && cr) System.out.println("You cannot go out"); else System.out.println("You can go out") `` ### Answer 1. The variable declaration of `dh` is setting the value of boolean to `True` which is incorrect and it should be `true` as Java is case-sensitive. 2. The print statement in `else` is missing and semicolon (`;`) at the end. Now, the fixed code is: ``java boolean dh = true; boolean cr= true; if (dh && cr) System.out.println("You cannot go out"); else System.out.println("You can go out");Sam executes the following program segment and the answer displayed is zero irrespective of any non zero values are given. Name the error. How the program can be modified to get the correct answer?
void triangle(double b, double h){ double a; a = 1 / 2 * b * h; System.out.println(“Area=”+a); }Answer
void triangle(double b, double h){ double a; a = 1.0 / 2.0 * b * h; System.out.println(“Area=”+a); }Explanation
The mathematical expression
1 / 2 * b * h, would be wrong in Java as the parametersbandhare both of typedoubleand the constants 1 and 2 areints. So, to fix it, we need to change 1 to 1.0 and 2 to 2.0 since the default data type for floating point numbers in Java isdouble.How many times will the following loop execute? What value will be returned?
int x=2; int y=50; do { ++x; y -= x++; } while(x<=10); return y;Answer
The loop will run 5 times. The value returned will be 15.
Write the output of the following String methods:
"ARTIFICIAL ".indexOf('I' )Answer
3
"DOG and PUPPY". trim().length()Answer
13
Name any two jump statements.
Answer
breakcontinue
(
returnis also a jump statement)Predict the output of the following code snippet:
String a="20"; String b="23"; int p=Integer.parseInt(a); int q=Integer.parseInt(b); System.out.print(a+"*"+b);Answer
20*23When there is no explicit initialization, what are the default values set for variables in the following cases?
Integer variable — 0
String variable —
null
int P [ ]={ 12,14,16,18}; int Q[ ]={ 20,22,24};Place all elements of P array and Q array in the array R one after the other.
Answer
Size of R will be length of P + length of Q which is 7.
Index of the first item/element will be
0and last will belength of R - 1which is7 - 1 = 6.