Java Quiz
Java Quiz
State Finished
Completed on Sunday, 17 March 2024, 4:07 PM
Time taken 2 mins 48 secs
Marks 5.00/7.00
Grade 71.43 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
To compile, and execute a program written in java, _______________ is
required.
Select one:
a.
JRE
b.
JVM
c.
JDK
d.
JIT
Feedback
Your answer is correct.
To compile and execute a Java program JDK is required. When the code is
already compiled and that byte code needs to be executed alone, then we
can have a JRE alone. JVM executes the byte code and produces the
output.
The correct answer is: JDK
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is Polymorphism?
Select one:
a.
ability to have many forms
b.
hiding the properties
c.
blueprint for an object
d.
ability to acquire the properties
Feedback
Your answer is correct.
The correct answer is: ability to have many forms
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Who is the father of Java?
Select one:
a.
Donald Knuth
b.
Jim Gray
c.
Dennis Ritchie
d.
James Gosling
Feedback
Your answer is correct.
The correct answer is: James Gosling
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
State true or false. Java is a structured programming language.
Select one:
True
False
Feedback
The correct answer is 'False'.
Question 5
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
JVM is independent of OS
Select one:
True
False
Feedback
Byte code is independent of OS. But JVM is dependent on the OS
The correct answer is 'False'.
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Java is _____________________________.
Select one:
a.
Platform dependent
b.
Platform independent
Feedback
Your answer is correct.
The correct answer is: Platform independent
Question 7
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
How was Java initially named?
Select one:
a.
The Oak
b.
Algol
c.
COBOL
d.
GreenTalk
Feedback
Your answer is incorrect.
The correct answer is: The Oak
Question 1
Correct
Mark 5.00 out of 5.00
Flag question
Question text
Question text
Arrange the below code in correct order :
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int num1 = sc.nextInt();
System.out.println("The value is "+num1);
}
}
Feedback
Your answer is correct.
Question 3
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Get two numbers, a and b from user. Fill the code to find the remainder
when a is divided by b.
Question text
Fill with the apt data type for the variable result.
public class Main
{
public static void main(String args[])
{
int number1=10;
double number2 = 54.5;
Answer result = number1 + number2;
}
}
Feedback
The correct answer is: double
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Fill the code to get the value for the variables based on their data type.
import java.util.Scanner ;
public class Main
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in) ;
int a = sc.nextInt() ;
float f = sc.nextFloat() ;
double salary=sc.nextDouble() ;
char gender=sc.next().charAt(0) ;
//String without space
String firstName=sc.next() ;
//String with space
String city=sc.nextLine() ;
}
}
Feedback
Your answer is correct.
The correct answer is:
Fill the code to get the value for the variables based on their data type.
[import java.util.Scanner];
public class Main
{
public static void main(String args[])
{
Scanner sc= [new Scanner(System.in)];
int a = [sc.nextInt()];
float f = [sc.nextFloat()];
double salary=[sc.nextDouble()];
char gender=[sc.next().charAt(0)];
//String without space
String firstName=[sc.next()];
//String with space
String city=[sc.nextLine()];
}
}
Thursday, 21 March
2024, 3:36 PM
State Finished
Completed on Thursday, 21 March 2024, 3:41 PM
Time taken 4 mins 56 secs
Marks 5.00/5.00
Grade 100.00 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Calculate sum
Get ‘n’ input from the user and find the sum of all those numbers.
When the input is provided as a negative number, the iteration should be
stopped and the sum needs to be printed.
Input : First input is the value of ‘n’. It should be followed by n numbers.
Stop getting input when a negative number is provided.
Output : The sum value.
Sample Input 1 :
6
12
22
23
45
6
14
Sample Output 1 :
122
Sample Input 2 :
6
12
22
23
-45
Sample Output 2 :
57
Explanation : Here n = 6. So loop should iterate 6 times. But 4 th input is -
45 which is negative. So the loop should terminate and print the sum of
12 + 22 + 23 as 57.
Rearrange the shuffled code.
import java.util.Scanner;
public class Test{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
int sum=0;
for(int i=1 ; i<=n ; i++ ) {
int value=sc.nextInt();
if(value < 0)
break;
sum = sum + value;
}
System.out.println(sum);
}
Feedback
Your answer is correct.
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Write a Java program to print the factors of a number, N got as input.
Sample Input :
12
Sample Output :
The factors of 12 are 1 2 3 4 6 12
You are provided with a shuffled code. Arrange in correct order.
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
System.out.print("The factors of "+n+" are ");
for(int i=1;i<=n;i++) {
if(n % i == 0) {
System.out.print(i+" ");
}
}
}
}
Feedback
Your answer is correct.
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Choose correct values the java code to print "hello" 10 times
public class Test {
public static void main(String args[])
{
int i ;
for( i=1 ; i<=10 ; i++ )
{
System.out.println("hello");
}
}
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Calculate Product
Get ‘n’ input from the user and find the product of all those numbers.
When the input is provided as a negative number, it should not be
considered for sum. But the iteration should be continue and finally the
sum needs to be printed.
Input : First input is the value of ‘n’. It should be followed by n numbers.
Output : The product value.
Sample Input 1 :
6
2
-22
14
4
-6
12
Sample Output 1 :
1344
Sample Input 2 :
4
-12
22
2
-45
Sample Output 2 :
44
Explanation : Here n = 4. So loop should iterate 4 times. But 2nd and
4th input are negative which should not be considered for product. Hence
the output is the product of 22 * 2 as 44.
Rearrange the shuffled code.
import java.util.Scanner;
Feedback
Your answer is correct.
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below code
public class Main {
public static void display(){
System.out.println("Hello");
}
public static void main(String a[]) {
display();
}
Please note and understand how this static method display is invoked. It
can be directly invoked inside main. Based on this understanding,
complete the below question.
Observe the java code to find the number of factors of a number got as
input from user.
In the main method invoke the countFactors method by passing the
relevant attribute. Store the returned value in a variable named "result".
import java.util.Scanner;
Question text
Write the correct order of the blocks to create the findSum method that
takes two integer as arguments and return an integer.
public class Main
{
publicintfindSumint a,int b int result = a + b;return
result; publicintfindSumint a,int b int result = a + b;return
result; publicintfindSumint a,int b int result = a + b;return
result; ( publicintfindSumint a,int b int result = a + b;return result; )
{
publicintfindSumint a,int b int result = a + b;return result;
publicintfindSumint a,int b int result = a + b;return result;
}
}
Feedback
Your answer is correct.
The correct answer is:
Write the correct order of the blocks to create the findSum method that
takes two integer as arguments and return an integer.
public class Main
{
[public] [int] [findSum] ( [int a,int b ] )
{
[int result = a + b;]
[return result;]
}
}
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Fill the code and write the signature of the public method display that
takes no arguments and returns no value.
public class Number
{
Answer
{
System.out.println("Hello");
}
}
Feedback
The correct answer is: public void display()
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Write a program to find whether a given integer is positive or negative.
Sample Input 1:
Enter the number:
45
Sample Output 1:
45 is a positive number
Sample Input 2:
Enter the number:
-364
Sample Output 2:
-364 is a negative number
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int num=sc.nextInt();
if(num>=0)
System.out.println(num+" is a positive number");
else
System.out.println(num+" is a negative number");
}
Feedback
Your answer is correct.
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below code snippet and choose the apt option
int a=25;
if( a%5 != 0a%5 = 0a%5 == 0 )
System.out.println(a+" is divisible by 5");
Feedback
Your answer is correct.
The correct answer is:
Observe the below code snippet and choose the apt option
int a=25;
if([a%5 == 0])
System.out.println(a+" is divisible by 5");
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
You are provided with the below code.
In main method invoke the findProduct method as instructed.
public class Calculator
{
public int findProduct(int x,int y)
{
return x*y;
}
Question text
A continue statement makes the execution jump to ______________.
Select one:
a.
the first statement of the loop
b.
the end of the loop
c.
the next iteration of the loop
d.
the statement just after continue
Feedback
Your answer is correct.
continue statement can be used in loops. It causes the loop to skip that
particular iteration and jump to the next iteration of the loop.
Question text
Identify the features of java.
Select one or more:
a.
Less security
b.
Multithreading
c.
Direct Access to memory using pointers
d.
Exception Handling
Feedback
Your answer is correct.
Few important features of java are object oriented programming
language, platform independent, architecture neutral, secure, robust
because of : exception handling, multi threading, no pointers, array limit
check etc.
The correct answers are: Exception Handling, Multithreading
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What will be the output of the program?
public class Sample {
final static short a = 2;
public static int b = 0;
public static void main(String [] args) {
for (int c=0; c < 4; c++)
{
switch (c) {
case a: System.out.print("a ");
default: System.out.print("default ");
case a-1: System.out.print("a-1 ");
break;
case a-2: System.out.print("a-2 ");
}
}
}
}
Select one:
a.
a-2 a-1 a default a-1 default a-1
b.
a default a-1
c.
a-2 a-1 a default default
d.
a-2 a-1 a default a-1
Feedback
Your answer is correct.
The value given for a case must be a literal or a constant. Variables are
not allowed. To terminate a case, use the break statement, which is
optional.
Here when c=0, it executes case a-2=2-2. When c=1, case a-1 = 2-1.
When c=2 and 3, default block is executed until it encounters break
statement.
The correct answer is: a-2 a-1 a default a-1 default a-1
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
The main method in java should ___________.
Select one:
a.
return int
b.
take boolean[] as argument
c.
be private static
d.
be public static
Feedback
Your answer is correct.
The correct answer is: be public static
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
The ________________ statement causes the program execution to stop and
JVM to shut down.
Select one:
a.
exit
b.
return
c.
break
d.
System.exit(0)
Feedback
Your answer is correct.
return statement is used to exit from a method. break statement is used
to exit from a loop or switch case. System.exit(0) to stop the execution
of the program.
The correct answer is: System.exit(0)
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
In a for loop, if the number of statements are greater than one, which of
the following needs to be inserted at the beginning and the end of the
loop?
Select one:
a.
Arrows<>
b.
Parenthesis()
c.
Square bracket [ ]
d.
French curly braces{ }
Feedback
Your answer is correct.
The correct answer is: French curly braces{ }
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Using Java we can develop ___________________.
Select one:
a.
Desktop Application
b.
Web Application
c.
Both the options
d.
None of these options
Feedback
Your answer is correct.
The correct answer is: Both the options
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
The break statement cannot be present for _____________ construct.
Select one:
while
if
do while
for
Feedback
Your answer is correct.
break statement can be used in loops and switch case only.
The correct answer is: if
Question 9
Partially correct
Mark 0.50 out of 1.00
Flag question
Question text
JRE comprises of ___________ and ___________.
Select one or more:
a.
API
b.
JDK
c.
JVM
d.
tools
Feedback
Your answer is partially correct.
You have correctly selected 1.
The correct answers are: API
, JVM
tarted on Friday, 5 April 2024, 6:21 PM
State Finished
Completed on Friday, 5 April 2024, 6:30 PM
Time taken 8 mins 29 secs
Marks 6.00/8.00
Grade 75.00 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What will be the output of the program?
public class Sample
{
public static void main(String [] args)
{
int i = 10;
do while ( i < 10 )
System.out.print("The value of i is " + i);
while ( i > 10 ) ;
}
}
Select one:
a.
Compilation error
b.
No output is produced.
c.
The value of i is 10 The value of i is 10
d.
The value of i is 10
Feedback
Your answer is correct.
The correct answer is: No output is produced.
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Who executes the byte code in java?
Select one:
a.
OS
b.
JDK
c.
JVM
d.
JRE
Feedback
Your answer is correct.
The correct answer is: JVM
Question 3
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Predict the output
int a=0;
if(a)
System.out.println( "Hello");
else
System.out.println( "Hai");
Select one:
a.
Need to include the braces { } for the code to work
b.
Compilation Fails
c.
Hello
d.
Hai
Feedback
Your answer is incorrect.
The correct answer is: Compilation Fails
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Which edition of java is used for developing web application?
Select one:
a.
J2ME
b.
J2EE
c.
J2SE
d.
All these options
Feedback
Your answer is correct.
The correct answer is: J2EE
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Which of the following options remain true for case constants in a switch
construct?
Select one:
a.
If no case matches but there is a default label, then all statements after
the matching default label in the switch block are executed in
sequence.
b.
The code with the switch construct gives a compilation error when there is
a duplicate case label
c.
If any one of the case constants has a match with the expression, then all
statements after the matching case label in the switch block are executed
in sequence
d.
If no case matches and there is no default label, then no further action
is taken and the switch statement completes abnormally
e.
If no case matches and there is no default label, then it will result in a
compilation error
Feedback
Your answer is correct.
The correct answers are: If no case matches but there is a default label,
then all statements after the matching default label in the switch block
are executed in sequence. , The code with the switch construct gives a
compilation error when there is a duplicate case label , If any one of the
case constants has a match with the expression, then all statements after
the matching case label in the switch block are executed in sequence
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
State True or False
For compiling a java code we have separate compilers for different OS.
Select one:
True
False
Feedback
The correct answer is 'False'.
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
__________ generates the byte code for a given file with .java extension.
Select one:
a.
JDK
b.
JVM
c.
JRE
Feedback
Your answer is correct.
The correct answer is: JDK
Question 8
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
In a nested for loop, the continue statement in an inner loop can be used
to bypass the current iteration of outerloop .
Select one:
True
False
Feedback
The correct answer is 'False'.
State True or False. When typing the code in code editor, it fixes the
compilation error. It also assists in how to fix that error.
Select one:
a.
False
b.
True
Feedback
The correct answer is: True
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
A project developed on one machine can be included in the current
workspace by ___________ that project.
Select one:
a.
Importing
b.
Exporting
Feedback
The correct answer is: Importing
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
In Eclipse, the plugin that is needed for Java Development is
______________.
Select one:
a.
JDT
b.
CDT
c.
PyDev
d.
JavaPlugin
Feedback
The correct answer is: JDT
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
We can move an already existing project in eclipse to another location by
compressing it. This we call as ________ the project.
Select one:
a.
Exporting
b.
Importing
Feedback
The correct answer is: Exporting
switch(a)
{
default:
System.out.println("Welcome");
}
Of which data types can the variable ‘a’ be?
1. long
2. byte
3. int
4. char
5. float
6. short
Select one:
a.
2,3, 4 and 6
b.
1 and 3
c.
3, 4 and 5
d.
3 and 4
Feedback
Your answer is correct.
The variable used in a switch statement can be a byte, short, int, char or
String type only.
Question text
In Eclipse IDE, if we provide a workspace, it should already exist. If not, it
will not open.
Select one:
True
False
Feedback
The correct answer is 'False'.
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
State True or False
When using eclipse whichever classes are needed for the present class
can be imported automatically.
Select one:
True
False
Feedback
The correct answer is 'True'.
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What will be the output of the following code?
int i=20;
if(i>10)
{
System.out.println( "The value of i is "+i);
i++;
if(i%2!=0)
break;
}
Select one:
a.
The value of i is 10
b.
Compilation fails
c.
The value of i is 20
d.
Code compiles but will not execute
Feedback
Your answer is correct.
break can be used only in loops or in switch case. When used in loop,
terminates the loop. When used with switch, control comes out of the
switch block.
The correct answer is:
Compilation fails
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Predict the output.
Question text
You are provided with the below code.
In main method invoke the findProduct method as instructed.
public class Calculator
{
public int findProduct(int x,int y)
{
return x*y;
}
Question text
Given class Student with attribute studentName. Fill the method
signature correctly to frame the getter method for the attribute.(Follow
camel case notation for the method).
public class Student
{
private String studentName ;
//Getter
publicStringgetStudentName( )return
studentName;System.out.println(studentObj.getStudentName());studentO
bj.getStudentName()voidthis.studentName = studentName;(String
studentName)setStudentNamestudentObj.setStudentName() publicStrin
ggetStudentName( )return
studentName;System.out.println(studentObj.getStudentName());studentO
bj.getStudentName()voidthis.studentName = studentName;(String
studentName)setStudentNamestudentObj.setStudentName() publicStrin
ggetStudentName( )return
studentName;System.out.println(studentObj.getStudentName());studentO
bj.getStudentName()voidthis.studentName = studentName;(String
studentName)setStudentNamestudentObj.setStudentName() publicString
getStudentName( )return
studentName;System.out.println(studentObj.getStudentName());studentO
bj.getStudentName()voidthis.studentName = studentName;(String
studentName)setStudentNamestudentObj.setStudentName()
{
publicStringgetStudentName( )return
studentName;System.out.println(studentObj.getStudentName());studentO
bj.getStudentName()voidthis.studentName = studentName;(String
studentName)setStudentNamestudentObj.setStudentName()
}
}
public class Main
{
public static void main(String args[])
{
Student studentObj = new Student();
studentObj.setStudentObj("Peter"); //Assume setter is written
//Print the name set using the setter
publicStringgetStudentName( )return
studentName;System.out.println(studentObj.getStudentName());studentO
bj.getStudentName()voidthis.studentName = studentName;(String
studentName)setStudentNamestudentObj.setStudentName()
}
}
Feedback
Your answer is correct.
The correct answer is:
Given class Student with attribute studentName. Fill the method
signature correctly to frame the getter method for the attribute.(Follow
camel case notation for the method).
public class Student
{
private String studentName ;
//Getter
Question 3
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Fill the appropriate code to create an object for Flight.
public class Flight {
public static void main(String a[]) {
Flight flightObj;
Flight flightObj=new Flight(); ;
}
}
flightObj=new Flight();
Feedback
Your answer is incorrect.
The correct answer is:
Fill the appropriate code to create an object for Flight.
public class Flight {
public static void main(String a[]) {
Flight flightObj;
[flightObj=new Flight();];
}
}
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below code. You are provided with a class Calculator. In the
Main class, create an object for the class Calculator with reference name
as "calcObj".
class Calculator
{
int num1;
int num2;
}
public class Main
{
public static void main(String args[])
{
Answer
}
}
Feedback
The correct answer is: Calculator calcObj=new Calculator();
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Given class Student with attribute studentName. Fill the method
signature correctly to frame the setter method for the attribute.(Follow
camel case notation for the method).
public class Student
{
private String studentName ;
//Setter
getStudentNamestudentObj.setStudentName("Peter");(String
studentName)return
studentName;studentObj.studentName="Peter";this.studentName =
studentName;studentName=studentNamepublicStringsetStudentName( )v
oidprivate getStudentNamestudentObj.setStudentName("Peter");(String
studentName)return
studentName;studentObj.studentName="Peter";this.studentName =
studentName;studentName=studentNamepublicStringsetStudentName( )v
oidprivate getStudentNamestudentObj.setStudentName("Peter");(String
studentName)return
studentName;studentObj.studentName="Peter";this.studentName =
studentName;studentName=studentNamepublicStringsetStudentName( )v
oidprivate getStudentNamestudentObj.setStudentName("Peter");(String
studentName)return
studentName;studentObj.studentName="Peter";this.studentName =
studentName;studentName=studentNamepublicStringsetStudentName( )v
oidprivate
{
getStudentNamestudentObj.setStudentName("Peter");(String
studentName)return
studentName;studentObj.studentName="Peter";this.studentName =
studentName;studentName=studentNamepublicStringsetStudentName( )v
oidprivate
}
}
Question text
Observe the below code. You are provided with a class Calculator. in
Calculator class, declare an attribute with name "result" of data type float
with default access specifier.
class Calculator
{
float num1;
float num2;
Answer
}
public class Main
{
public static void main(String args[])
{
Calculator calcObj=new Calculator();
}
}
Feedback
The correct answer is: float result;
Observe the below code. You are provided with a class Calculator. in
Calculator class, declare an attribute with name "result" of data type float
with default access specifier.
class Calculator
{
float num1;
float num2;
Answer
}
public class Main
{
public static void main(String args[])
{
Calculator calcObj=new Calculator();
}
}
Feedback
The correct answer is: float result;
Question 2
Partially correct
Mark 18.00 out of 19.00
Flag question
Question text
Code :
class Trainee {
private int traineeId;
private String traineeName;
private char gender;
private float rating;
public Trainee(int traineeId, String traineeName, char gender, float
rating) {
this.traineeId = traineeId;
this.traineeName = traineeName;
= gender;
= rating;
}
public int getTraineeId() {
return traineeId;
}
public void setTraineeId(int traineeId) {
this.traineeId = traineeId;
}
public String () {
return traineeName;
}
public void ( traineeName) {
}
public char () {
return gender;
}
public void ( ) {
this.gender=gender;
}
public getRating() {
}
public void setRating(float rating) {
this.rating=rating;
}
public void displayTrainee() {
System.out.println("Trainee Details");
//To print use attribute name and not the getters
System.out.println("ID is "+ );
System.out.println("Name is "+ );
System.out.println("Gender '"+ +"'");
System.out.println("Rating is "+ );
}
}
class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in) ;
System.out.println("Enter the ID");
int id =
System.out.println("Enter the name");
String name = sc.next();
System.out.println("Enter the gender");
char gender=#sc.next().charAt(0);#
System.out.println("Enter the rating");
float rating = sc.nextFloat();
//Create the object
Trainee tObj =
//Invoke the displayTrainee method
}
}
Feedback
Your answer is partially correct.
Question 3
Partially correct
Mark 0.09 out of 1.00
Flag question
Question text
Arrange the code shuffle provided such that
- first you provide the no argument constructor that prints "In default
constructor" ,
- next is the constructor with id, name and salary as arguments that
invokes the no arg constructor and sets the attributes passed as attribute
and
- finally the constructor with id, name, salary and pancardno as
arguments that invokes the 3 argument constructor and then sets the
pancardno.
public Employee(){
System.out.println("In default constructor");
}
public Employee(int id, String name, float salary) {
this();
this.id = id;
this.name = name;
this.salary = salary;
}
public Employee(int id, String name, float salary, String
pancardno) {
this(id,name,salary);
this.pancardno = pancardno;
}
Feedback
Your answer is partially correct.
The correct order for these items is as follows:
11. }
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Assume there is a class Student with attributes, id, name and cgpa.
Assume there is a constructor with attributes id and name.
You are provided with the three argument constructor shuffled.
Rearrange it in correct order.
Feedback
Your answer is correct.
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below class and match the output for the code snippets
provided.
public class Product
{
//Attributes
private int productId;
private String productName;
private float price;
private char category;
//Constructor
public Product(){
}
public Product(int productId,String productName,float price)
{
this.productId=productId;
this.productName=productName;
this.price=price;
}
Product p = new
Product(501,"Wallet",520); Answer 3Choose...501 Wallet 520.071 Mobile
p.display(); 80000 A0 null 0.0
Feedback
Your answer is correct.
The correct answer is: Product p = new Product()
p.display(); → 0 null 0.0, Product p = new Product(71,"Mobile",80000,'A');
p.display(); → 71 Mobile 80000 A, Product p = new
Product(501,"Wallet",520);
p.display(); → 501 Wallet 520.0
Question 6
Partially correct
Mark 0.10 out of 1.00
Flag question
Question text
Rearrange the shuffled code provided in the below order
- Define the class
- Define the method findProduct with 2 attributes
- Define the method findProduct with 3 attributes
- Define the main method that invokes findProduct method with 2
attributes and then invokes the findProduct method with 3 attributes.
Feedback
Your answer is partially correct.
The correct order for these items is as follows:
Question text
Match the constructor Types based on Objects created.
When constructor is provided by the Answer 1Choose...No argument
developer for the class Employee constructorCompilation errorParameterized
ConstructorDefault constructor
Employee e = new Employee();
Question text
You are provided with the sample code for the class Employee.
Create a constructor for the class Employee, so that, when an object is
created for the class Employee, it should print the message as "In Default
constructor".
public class Employee
{
Answer
{
System.out.println("In Default constructor");
}
public static void main(String args[])
{
Employee empObj = new Employee();
}
}
Feedback
The correct answer is: public Employee()
Started on Sunday, 28 April 2024, 3:37 PM
State Finished
Completed on Sunday, 28 April 2024, 3:40 PM
Time taken 3 mins 49 secs
Marks 3.00/3.00
Grade 100.00 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
You are provided with a class Pet which has an attribute petName. As the
objects are created the count should be incremented.
Design the class correctly for the above purpose.
class Pets
{
private String petName;
private static int count;
public void setPetName(String petName) {
this .petName = petName;
}
public static void setCount(int count) {
Pets .count = count;
}
}
private private static void static void this Pets
Feedback
Your answer is correct.
The correct answer is:
You are provided with a class Pet which has an attribute petName. As the
objects are created the count should be incremented.
Design the class correctly for the above purpose.
class Pets
{
[private] String petName;
[private static] int count;
public [void] setPetName(String petName) {
[this].petName = petName;
}
public [static void] setCount(int count) {
[Pets].count = count;
}
}
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Fill the apt code to invoke the addNumbers method, by passing
num1 and num2 as attributes.
import java.util.*;
public class Test
{
public static int addNumbers(int a,int b)
{
return a+b;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num1=sc.nextInt();
int num2=sc.nextInt();
Test testObj = new Test();
int result = Test.addNumbers(num1,num2) ;
}
}
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
To automate the employee management system of ABC Bank, the below
mentioned class Employee is written. Identify the instance variables and
class variables and provide the appropriate modifier.
Code :
public class Employee
{
private int employeeId;
private String employeeName;
private double salary;
private static String companyName;
}
private private static
Feedback
Your answer is correct.
The correct answer is:
To automate the employee management system of ABC Bank, the below
mentioned class Employee is written. Identify the instance variables and
class variables and provide the appropriate modifier.
Code :
public class Employee
{
[private] int employeeId;
[private] String employeeName;
[private] double salary;
[private static] String companyName;
}
Started on Sunday, 28 April 2024, 8:53 PM
State Finished
Completed on Sunday, 28 April 2024, 8:59 PM
Time taken 5 mins 50 secs
Marks 6.00/8.00
Grade 75.00 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Remove flag
Question text
package com.main;
import java.util.*;
public class Main {
public static void main(String args[]) {
List<Integer> list=new ArrayList<Integer>();
Date d=new Date();
}
}
The above class should be in com.main package.
Drag and drop the correct statements in the above code.
import com.main.*; import java.util.Date; import java.util.ArrayList; imp
ort java.util.List;
Feedback
Your answer is correct.
The correct answer is:
[package com.main;]
[import java.util.*;]
public class Main {
public static void main(String args[]) {
List<Integer> list=new ArrayList<Integer>();
Date d=new Date();
}
}
The above class should be in com.main package.
Drag and drop the correct statements in the above code.
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
public class Main {
public static void main(String args[]) {
int x = Integer.parseInt("5.6");
}
}
Drag and drop the correct code which will cause NumberFormatException
when the above code is compiled.
double pi=Double.parseDouble("3.14"); boolean status=Boolean.parseB
oolean("TRUE");
Feedback
Your answer is correct.
The correct answer is:
public class Main {
public static void main(String args[]) {
[int x = Integer.parseInt("5.6");]
}
}
Drag and drop the correct code which will cause NumberFormatException
when the above code is compiled.
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
import ;
public class Main {
public static void main(String args[]) {
double result1 = abs(-5);
double result2 = sqrt(225);
}
}
Question 4
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
public class Test
{
public static void main(String args[])
{
ArrayListScannerjava.util.Scannerjava.util.ArrayList
sc=new ArrayListScannerjava.util.Scannerjava.util.ArrayList (System.in);
ArrayListScannerjava.util.Scannerjava.util.ArrayList
list=new ArrayListScannerjava.util.Scannerjava.util.ArrayList ();
}
}
Feedback
Your answer is incorrect.
The correct answer is:
public class Test
{
public static void main(String args[])
{
[java.util.Scanner] sc=new [java.util.Scanner](System.in);
[java.util.ArrayList] list=new [java.util.ArrayList]();
}
}
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Code :
Scanner sc = new Scanner(System.in);
String str = sc.next();
Match the options correctly.
float Answer
f = 1Choose...Integer.parseInt(str);str[0];Float.parseFloat(str);Boolean.parseBoolean(str);st
r.charAt(0);Character.parseChar(str);
char Answer
c = 2Choose...Integer.parseInt(str);str[0];Float.parseFloat(str);Boolean.parseBoolean(str);st
r.charAt(0);Character.parseChar(str);
bool
ean Answer
flag 3Choose...Integer.parseInt(str);str[0];Float.parseFloat(str);Boolean.parseBoolean(str);st
= r.charAt(0);Character.parseChar(str);
int x Answer
= 4Choose...Integer.parseInt(str);str[0];Float.parseFloat(str);Boolean.parseBoolean(str);st
r.charAt(0);Character.parseChar(str);
Feedback
Your answer is correct.
The correct answer is: float f = → Float.parseFloat(str);, char c = →
str.charAt(0);, boolean flag = → Boolean.parseBoolean(str);, int x = →
Integer.parseInt(str);
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
public class Main {
public static void main(String args[]) {
System.out.println(args.length);
}
}
When the above code is executed as "java Main" what will be the
output ?
Select one:
a.
null
b.
1
c.
0
d.
ArrayIndexOutOfBoundsException
Feedback
Your answer is correct.
The correct answer is: 0
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Line 1 : int x = Integer.parseInt("5");
Line 2 : float pi=Float.parseFloat("3.14");
Line 3 : boolean status=Boolean.parseBoolean("TRUE");
Line 4 : char c = new Character("X");
Which of the above lines can cause compilation error?
Select one:
a.
Line 1
b.
Line 3
c.
Line 4
d.
Line 2
Feedback
Your answer is correct.
The correct answer is: Line 4
Question 8
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Assume we have the class Product as shown below
package com.model;
public class Product
{
//some code
public void display() {
System.out.println("Display method");
}
}
Observe the code given below
public class Main
{
public static void main(String args[])
{
Answer
productObj.display();
}
}
Fill the appropriate code, so that productObj is a reference of Type
Product and the above code displays the output as "Display method".
Feedback
The correct answer is: com.model.Product productObj=new
com.model.Product();
Started on Sunday, 28 April 2024, 9:04 PM
State Finished
Completed on Sunday, 28 April 2024, 9:11 PM
Time taken 6 mins 19 secs
Marks 7.67/8.00
Grade 95.83 out of 100.00
Feedback Congratulations!!! You have passed by securing more than 70%
Question 1
Partially correct
Mark 0.67 out of 1.00
Flag question
Question text
Analyze the below program, and fill the correct code so that it produces
the below output: 0 101
class Book {
private int bookId;
return bookId;
this.bookId = bookId;
return bookPrice;
this.bookPrice = bookPrice;
System.out.println(bobj.getBookId());
bobj.setBookId(101);
System.out.println(bobj.getBookPrice());
Note : Same drag option can be used multiple times. Analyse and use the
correct option
System.out.println(bobj.getBookId()); bobj.setBookId(101); System.out.pri
ntln(bobj.getBookPrice());
Feedback
Your answer is partially correct.
Once we create an object, the default value will be assigned for each
attribute. First print the values, then set the values and again print the
values.
You have correctly selected 2.
The correct answer is: Analyze the below program, and fill the correct
code so that it produces the below output: 0 101
class Book {
return bookId;
this.bookId = bookId;
return bookPrice;
this.bookPrice = bookPrice;
[System.out.println(bobj.getBookId());]
[bobj.setBookId(101);]
[System.out.println(bobj.getBookId());]
Note : Same drag option can be used multiple times. Analyse and use the
correct option
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Consider the below code snippet and determine the output.
class Student
{ private int studentId;
private float average;
}
class Test
{
public static void main(String a[])
{
Student s=new Student();
s.studentId=123;
System.out.println(s.studentId);
}
}
Select one:
a.
1
b.
0
c.
Any value
d.
Compile time error
Feedback
Your answer is correct.
Private variables can be accessed only within the class. They cannot be
accessed outside the class.
The correct answer is: Compile time error
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
The below code snippet shows an error
cannot find symbol:
System.out.println("BookId:"+bobj.getId());
public class Book {
private int bookId;
private double bookPrice;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
}
public class Test {
public static void main(String[] args) {
Book bobj=new Book();
bobj.setBookId(123);
bobj.setBookPrice(500);
System.out.println("BookId:"+bobj.getId());
System.out.println("BookPrice:"+bobj.getBookPrice());
}
}
Analyze the above code and select the correct reason for the error.
Select one:
a.
bobj is not initialized
b.
“+” symbol should not be used in System.out.println
c.
Getter method should not be called inside System.out.println
d.
getId method is not present in the book class
Feedback
Your answer is correct.
The correct answer is: getId method is not present in the book class
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Arrange the code in the correct sequence, so that the program compiles
successfully.
Feedback
Your answer is correct.
The purpose of the setter method is to set a valid value for the attribute,
by doing the necessary validations.
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
___ and _____ are the access specifiers that can be applied to top level
Class.
Select one or more:
a.
protected
b.
public
c.
virtual
d.
default
Feedback
Your answer is correct.
The correct answers are: default, public
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
class Sample{
private double num = 100;
private int square(int a){
return a*a;
}
}
public class Test{
public static void main(String args[]){
Sample obj = new Sample();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
Select one:
a.
Executes but no output
b.
100
c.
Run time error
d.
Compile time error
Feedback
Your answer is correct.
The correct answer is: Compile time error
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Choose the appropriate access specifier for the attribute value so that it
can be accessed from anywhere.
class Test
{
privateprotectedpublicdefault int value;
}
Feedback
Your answer is correct.
The correct answer is:
Choose the appropriate access specifier for the attribute value so that it
can be accessed from anywhere.
class Test
{
[public] int value;
}
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Choose the appropriate return type for the getters and setters provided
below.
class Test
{
private int value;
public voidint setValue(int value){//some code}
public voidint getValue(){//some code}
}
Feedback
Your answer is correct.
The correct answer is:
Choose the appropriate return type for the getters and setters provided
below.
class Test
{
private int value;
public [void] setValue(int value){//some code}
public [int] getValue(){//some code}
}
Started on Sunday, 28 April 2024, 9:11 PM
State Finished
Completed on Sunday, 28 April 2024, 9:14 PM
Time taken 2 mins 51 secs
Marks 4.33/5.00
Grade 86.67 out of 100.00
Feedback Congratulations!!! You have passed by securing more than 70%
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Drag and Drop the code so that the constructor for Student class is
overloaded correctly.
public class Student {
private int studentId;
private String name;
private float grade;
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below code.
Feedback
Your answer is correct.
The correct answer is:
Observe the below code.
Question 3
Partially correct
Mark 0.33 out of 1.00
Flag question
Question text
You are given with few classes.
Match the code with the type of constructor available in the class.
Feedback
Your answer is partially correct.
You have correctly selected 1.
The correct answer is: public class Student {
public Student(int studentId, String name) {
} → Parameterized Constructor,
public class Student {
} → Default constructor,
public class Student {
public Student() {
} → No-argument Constructor
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the code below.
public class Student {
int studentId;
String name;
char grade;
Select one:
a.
Compilation error because of the parameter – mark - in constructor. It
should be grade instead of mark.
b.
Compiles successfully
c.
Compilation error because cannot call methods from constructor
Feedback
Your answer is correct.
The correct answer is:
Compiles successfully
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below class.
class Product{
int productId;
String productName;
Product() {
productId=0; productName=” ”;
}
Product(int id, String name) {
//access Account() ---- Line 1
productId=id;
productName=name;
}
}
Identify the valid option which is used to invoke the no argument
constructor, Product(), at Line 1.
Select one:
a.
Product();
b.
Product
c.
super();
d.
this();
Feedback
this() invokes the current object's no argument constructor.
The correct answer is: this();
Sunday, 28 April
2024, 9:20 PM
State Finished
Completed on Sunday, 28 April 2024, 9:22 PM
Time taken 2 mins 10 secs
Marks 3.67/5.00
Grade 73.33 out of 100.00
Feedback Congratulations!!! You have passed by securing more than 70%
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the below code
public class Product
{
int productId;
String productName;
static int count = 0;
Feedback
The value of the static count variable is incremented by 1 after each
object creation. So a simple print statement displaying count would
suffice.
The correct answer is:
Observe the below code
public class Product
{
int productId;
String productName;
static int count = 0;
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Choose the correct option :
public class Flight{
int flightId;
static int noOfSeats;
public static void display(){
System.out.println("No of seats
"+noOfSeats);System.out.println("Flight ID"+flightId);
}
}
Feedback
Non static members cannot be accessed from static methods. If we want
to access non static members from a static method it is possible by
creating an object. Hence, only "noOfSeats" can be accessed, not
"flightId".
}
}
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Match the following :
static
method Answer 1Choose...Can access non static members onlyCan access both static
and non static membersCan access static members only
non static Answer 2Choose...Can access non static members onlyCan access both static
method and non static membersCan access static members only
Feedback
Your answer is correct.
The correct answer is:
static method → Can access static members only, non static method →
Can access both static and non static members
Question 4
Partially correct
Mark 0.67 out of 1.00
Flag question
Question text
Observe the code :
public class Employee {
String name;
static int employeeCount;
//Line 1
}
Which of the following code can be included in Line 1?
Select one or more:
a.
public static void display() {
System.out.println("Employee Name"+name);
}
b.
public static void display(){
System.out.println("Employee count "+employeeCount);
}
c.
public void display(){
System.out.println("Employee Name"+name);
}
d.
public void display(){
System.out.println("Employee count "+employeeCount);
}
Feedback
From a static method, we can access only static members. Non static
members cannot be accessed from static methods.
However, from a non static method, we can access both static and non
static members.
You have correctly selected 2.
The correct answers are:
public static void display(){
System.out.println("Employee count "+employeeCount);
},
public void display(){
System.out.println("Employee count "+employeeCount);
},
public void display(){
System.out.println("Employee Name"+name);
}
Question 5
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
For the below code, what are the valid ways to invoke display method in
the main method.
public class Test {
public static void display(){
}
}
public class Main {
public static void main(String a[]){
//Invoke the display method
}
}
Select one or more:
a.
new Test().display();
b.
display();
c.
Test.display();
Feedback
Static method can be invoked either by using the object instance or using
the class name.
The correct answers are: Test.display();, new Test().display();
Started on Sunday, 28 April 2024, 9:22 PM
State Finished
Completed on Sunday, 28 April 2024, 9:25 PM
Time taken 2 mins 51 secs
Marks 4.00/5.00
Grade 80.00 out of 100.00
Feedback Congratulations!!! You have passed by securing more than 70%
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Given the below code :
import static java.lang.Math.PI;
public class AreaCalculator {
public double calculateArea(double radius) {
double area = PI * radius * radius; //Instead of Math.PI
return area;
}
}
Drag and drop the correct statement to use the static variable PI.
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Assume class Calculator in package p1 and CalculatorService class in
package p2 as shown below.
package p1;
public class Calculator {
__________ static void calculate(){
//some code here
}
}
package p2;
import p1.Calculator;
public class CalculatorService {
public void display(){
Calculator.calculate();
}
}
What can be the valid access specifier for the calculate method in
Calculator class so that it can be accessed from CalculatorService class?
Select one:
a.
default
b.
Any access specifier except private
c.
public
d.
private
e.
protected
Feedback
If a method defined within a class in one package has to be invoked from
outside, then that method has to be declared public.
The correct answer is: public
Question 3
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Given the class Book in packages p1 and class Main in package p2. In
main to create an object of Book, which of the following are valid.
Select one:
a.
Book bookObj=new Book();
b.
p1.Book bookObj=new Book();
c.
p1.Book bookObj=new p1.Book();
Feedback
Since the Book class is present within p1 package, it has to be accessed
as "p1.Book", everytime, from package p2.
The correct answer is: p1.Book bookObj=new p1.Book();
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Given the class Book and Library in two different packages :
1. package model;
2. public class Book {
3. private static void countBook() { }
4. }
1. package util;
2. public class Library {
3. public static void main(String[] args) {
4. // insert code here
5. }
6. }
What is required at line 4 in class Library to use the countBook method of
Book class?
Select one:
a.
util.Library.countBook();
b.
Library class cannot use the countBook method in Book class.
c.
countBook();
d.
model.Book.countBook();
e.
Book.countBook();
Feedback
countBook() cannot be invoked since the method is declared private.
The correct answer is: Library class cannot use the countBook method in
Book class.
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Rearrange this code correctly.
package test;
import java.util.Scanner;
public class Main
{
//Some code here
}
Feedback
Your answer is correct.
Started on Sunday, 28 April 2024, 9:26 PM
State Finished
Completed on Sunday, 28 April 2024, 9:30 PM
Time taken 4 mins 30 secs
Marks 6.00/8.00
Grade 75.00 out of 100.00
Question 1
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Given:
public class Message {
String msg;
int noOfWords;
public Message() {
msg += " Thank you";
}
public Message(int noOfWords) {
this.noOfWords = noOfWords;
msg = "Welcome";
Message();
}
public static void main(String args[]) {
Message m = new Message(5);
System.out.println(m.msg);
}
}
What will be the output ?
Select one:
a.
The code runs with no output
b.
Welcome Thank you
c.
Welcome
d.
An exception is thrown at runtime
e.
Compilation fails
f.
Welcome Thank you 5
Feedback
Your answer is incorrect.
To invoke one constructor from other constructor, use this() and also it
should be the first statement in a constructor.
Question text
Integer x1 = new Integer(120);
int x2 = 120;
System.out.println( x1 == x2 );
Question text
The methods of a class with the ____________ access specifier cannot be
accessed in another class of different package.
Select one:
a.
private
b.
protected
c.
public
d.
default
Feedback
Your answer is correct.
Question text
Predict the output.
class X
{
void display(int a)
{
System.out.println("INT");
}
void display(double d)
{
System.out.println("DOUBLE");
}
}
Select one:
a.
Ambiguity error
b.
Compilation Fails
c.
DOUBLE
d.
INT
Feedback
Your answer is correct.
The correct answer is: INT
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Observe the code
Select one:
a.
display x = 7 main x = 7
b.
Compilation fails.
c.
display x = 6 main x = 7
d.
An exception is thrown at runtime.
e.
display x = 6 main x = 6
f.
display x = 7 main x = 6
Feedback
Your answer is correct.
The correct answer is: display x = 6 main x = 6
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is the outcome of the code?
Select one:
a.
Compilation fails
b.
Scrumdiddlyumptious
c.
Gobstopper
Fizzylifting
d.
Gobstopper
Scrumdiddlyumptious
e.
Scrumdiddlyumptious
Fizzylifting
Feedback
Your answer is correct.
In modifyDesc method, the Item reference passed as parameter gets
reinitialized to a new item object. Hence the description of the
referece “it” does not change.
Question text
Which of the following is not a Java modifier?
Select one:
a.
public
b.
protected
c.
virtual
d.
private
Feedback
Your answer is correct.
The correct answer is:
virtual
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Given classes defined in two different files:
1. package p1;
2. public class Test {
3. public static void display(String [] a) { /* some code */ }
4. }
1. package p2;
2. public class TestMain {
3. public static void main(String[] args) {
4. String [] names = new String[10];
5. // insert code here
6. }
7. }
Select one:
a.
p1.Test.display(names);
b.
p1.display(names);
c.
display(names);
d.
TestMain cannot use methods in p1
e.
import p1.Test.*; display(names);
Feedback
Your answer is correct.
Question text
Identify the true statement(s).
Statement 1 : When no constructor is written in a class, the compiler
creates a default constructor
Statement 2 : The default constructor will implicitly invoke the default /
no-argument constructor of the super class
Statement 3 : If a class has a parametrized constructor alone, then the
compiler will create the default constructor
Statement 4 : If a class has a parametrized constructor, it is mandatory
to write a no-argument constructor
Select one:
a.
2 and 3
b.
1, 2 and 4
c.
1, 2 and 3
d.
1 and 2
Feedback
Your answer is correct.
In a class, if no constructor is written, constructor provides the default
constructor. Instead, if a parameterized constructor is written by the
developer, compiler will not provide default constructor. If needed,
developer should write a no argument constructor. In case of inheritance,
child class default / no argument constructor will implicitly invoke the
default / no-argument constructor of the super class.
The correct answer is:
1 and 2
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Given:
public class ItemTest
{
private final int id;
public ItemTest(int id) {
this.id = id;
}
public void updateId(int newId) {
id = newId;
}
public static void main(String[] args) {
ItemTest fa = new ItemTest(42);
fa.updateId(69);
System.out.println(fa.id);
}
}
What is the result?
Select one:
a.
Runtime Error
b.
CompileTime Error
c.
69
Feedback
Your answer is correct.
Question text
Predict the Output of following Java Program.
class Test {
int x = 10;
public static void main(String[] args) {
System.out.println(x);
}
}
Select one:
a.
Compile Time Error
b.
Runtime Exception
c.
10
d.
0
Feedback
Your answer is incorrect.
main method is static. In static method, we can access only static
members. To access non static members in a static context, it is possible
only through objects.
The correct answer is: Compile Time Error
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Identify which statement is true about construtors.
Select one:
a.
Constructor of a class should not have a return type, which means the
return type is void
b.
Constructor will be invoked explicitly like other methods
c.
Constructor should have same name as class name, but not case sensitive
d.
Constructor can be overloaded
Feedback
Your answer is correct.
Rules for writing constructor is : It should have the same name as class
name, case sensitive. It should not have any return type, not even void.
It cannot be invoked like normal methods. It gets invoked automatically
when object is created. Constructor can be overloaded.
The correct answer is: Constructor can be overloaded
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
A Java class has the following field:
private boolean enabled;
Which two pairs of method declarations follow the Java standard for
accessing this field? (Choose two.)
Select one or more:
a.
public void setEnabled( boolean enabled )
public boolean getEnabled()
b.
public void setEnabled( boolean enabled )
public boolean isEnabled()
c.
public boolean setEnabled( boolean enabled )
public boolean getEnabled()
d.
public void setEnabled( boolean enabled )
public void isEnabled()
Feedback
Your answer is correct.
When writing getters and setters, setters return type is void and getters
return type is the corresponding data type. Naming convention is
camelcase notation. For setters start with set followed by field name and
for getters start with get followed by field name. For boolean return type,
it should start with 'is' or 'are' followed by field name.
The correct answers are: public void setEnabled( boolean enabled )
public boolean isEnabled(), public void setEnabled( boolean enabled )
public boolean getEnabled()
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Which members of a class can be accessed by other classes is determined
by the ________________
Select one:
a.
constructor
b.
Access specifier
c.
variables
d.
class
Feedback
Your answer is correct.
Access Specifier restricts the access to classes, fields and methods as
either within the class or within the package or by the sub classes.
Question text
package edu.ABC.model;
public class Account {
public static final float INTERTEST_RATE = 7.5;
}
Question text
What does this() mean in constructor chaining concept?
Select one:
a.
Used for calling the current object of the parent class.
b.
Used for calling the parameterized constructor of the parent class.
c.
Used for calling the no argument constructor of the same class.
d.
Used for calling the current object of the same class.
Feedback
Your answer is correct.
When constructor chaining happens within the same class, this() is used
to invoke the no argument constructor from a parameterized constructor.
Question text
}
Question 2
Correct
Mark 5.00 out of 5.00
Flag question
Question text
int arr [ ] new [10]
Question 3
Partially correct
Mark 4.00 out of 7.00
Flag question
Question text
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the
array");
int n = sc.nextInt();
//Declare the array
int arr[ ]=new int[n];
//Get the elements from user
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
//Code to find the sum of odd elements in the array
int sum=findSumOddElements(#arr# ); // invoke the method
Question text
Which of the below statements correctly initializes an array ?
Select one or more:
a.
double mark[ ]={5.5,8.5,9.5,3.5,2.5};
b.
double mark[ ]=new double[ ]{5.5,8.5,9.5,3.5,2.5};
c.
double mark[5]={5.5,8.5,9.5};
d.
double mark[5]={5.5,8.5,9.5,3.5,2.5};
Feedback
Your answer is correct.
The correct answers are: double mark[ ]={5.5,8.5,9.5,3.5,2.5};, double
mark[ ]=new double[ ]{5.5,8.5,9.5,3.5,2.5};
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Tom wants to store the temperature of 15 cities. Help him to declare the
array to do this.
Which of the following declaration meets his requirement?
Select one:
a.
float temperature[]=new float(15);
b.
float temperature[]=new temperature[15];
c.
float temperature[15];
d.
float temperature(15)
e.
float temperature[]=new float[15];
Feedback
Your answer is correct.
The correct answer is: float temperature[]=new float[15];
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Find below the shuffled code to find the minimum number in the array.
Arrange it in correct order.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int n = sc.nextInt();
int arr[]=new int[n];
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
int minimum=arr[0];
for(int i=0;i<n;i++)
{
if(arr[i]<minimum) {
minimum=arr[i];
}
}
System.out.println("Minimum element is "+minimum);
}
}
Feedback
Your answer is correct.
Question 7
Partially correct
Mark 0.79 out of 1.00
Flag question
Question text
For the below problem statement, you are provided with a shuffled code.
Arrange them in correct order.
Problem Statement :
Peter has started to learn arrays. He needs to get 2 arrays of same size
from the user and find the sum of corresponding elements in 2 arrays and
print the result.
Input has 2n+1 numbers. The first input is ‘n’ , the size of the array. The
next ‘n’ input represents the elements in the first array. The last 'n'
integers represents the elements in the second array.
Sample Input 1:
4
2
3
6
8
3
4
1
2
Sample Output 1:
5 7 7 10
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,n,a[],b[];
System.out.println("Enter the number of elements in the array");
n = sc.nextInt();
a = new int[n];
b = new int[n];
for(i=0;i<n;i++)
a[i]=sc.nextInt();
for(i=0;i<n;i++)
{
b[i]=sc.nextInt();
a[i]=a[i]+b[i];
for(int x : a)
System.out.print(x+" ");
}
}
Feedback
Your answer is partially correct.
The correct order for these items is as follows:
1. import java.util.Scanner;
2. public class Test {
3. public static void main(String[] args) {
4. Scanner sc=new Scanner(System.in);
int i,n,a[],b[];
5. System.out.println("Enter the number of elements in the array");
n = sc.nextInt();
6. a = new int[n];
b = new int[n];
7. for(i=0;i<n;i++)
a[i]=sc.nextInt();
8. for(i=0;i<n;i++)
{
9. b[i]=sc.nextInt();
10. a[i]=a[i]+b[i];
11. }
12. for(int x : a)
13. System.out.print(x+" ");
14. }
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Fill the code to print the numbers in the array using for each loop
public class Main
{
public static void main(String args[])
{
int arr[]={50,70,80,90,100};
//fill code to print the array
for(Answer)
System.out.println(num);
}
}
Feedback
The correct answer is: int num:arr
Started on Sunday, 28 April 2024, 10:02 PM
State Finished
Completed on Sunday, 28 April 2024, 10:11 PM
Time taken 9 mins 9 secs
Marks 16.75/17.00
Grade 98.53 out of 100.00
Question 1
Correct
Mark 2.00 out of 2.00
Flag question
Question text
Fill the code to convert int to String and then String to int.
Code :
int x = 9803;
//Convert int to String
String str1 = ;
System.out.println(str1);
String str2 = "1894";
//Convert string str2 to int
int z = ;
System.out.println(z);
System.out.println(str);
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Fill the below code to compare two strings and print "yes" if they are same
else print "no".
Don't consider the case. Example, if the strings are "Good" and "good"
the output should be "yes".
Code :
Scanner sc=new Scanner(System.in);
String s1=sc.next();
String s2=sc.next();
if( )
System.out.println("yes");
else
System.out.println("no");
Question 3
Correct
Mark 2.00 out of 2.00
Flag question
Question text
Tom is playing scrabble and forms new words. He wants to count the
number of characters present in the word formed by him. Help him to do
this using the below java program.
Code :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
//find length and print the same
int len= ;
System.out.println("The length of the word is "+ ) ;
}
}
Question 4
Correct
Mark 5.00 out of 5.00
Flag question
Question text
Raghu's tutor provides him a simple sentence and asks him to count the
number of words in the sentence. The words are separated by space.
Example, if the tutor provides the sentence as "Welcome to Java", the
output should be
Number of words 3
The words are
Welcome
to
Java
Fill the space in the below code so that the code fulfills his requirement.
Code :
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String words[ ] = ;
int count= ; //count holds the number of words in the
sentence
System.out.println("Number of words "+ );
System.out.println("The words are ");
for(String : ) //for each loop
System.out.println(x);
Question 5
Correct
Mark 3.00 out of 3.00
Flag question
Question text
Question text
In the below code snippet, fill the code to get the character array for a
given string, str.
Code :
Scanner sc=new Scanner(System.in);
String str=sc.next();
char c[] = ;
for(char x : c )
System.out.print(x+" ");
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
The code snippet below takes a character as input and prints as
"alphabet", "digit" or "special character" depending on character provided
as input.
Choose the apt option to do this.
Scanner sc=new Scanner(System.in);
char ch = sc.next().charAt(0);
if( ch..isAlphabet()ch..isAlphabetic()Character.isAlphabet(ch)Character.isAl
phabetic(ch)Character.isDigit(ch)ch.isDigit() )
System.out.println(ch+" is an alphabet");
else
System.out.println(ch+" is a special character");
Feedback
Your answer is correct.
The correct answer is:
The code snippet below takes a character as input and prints as
"alphabet", "digit" or "special character" depending on character provided
as input.
Choose the apt option to do this.
Scanner sc=new Scanner(System.in);
char ch = sc.next().charAt(0);
if([Character.isAlphabetic(ch)])
System.out.println(ch+" is an alphabet");
else if([Character.isDigit(ch)])
System.out.println(ch+" is a digit");
else
System.out.println(ch+" is a special character");
Question 8
Partially correct
Mark 0.75 out of 1.00
Flag question
Question text
Fill the code to split the words in a String and print them
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String name= sc.nextLine()namewordsname.split("
")name.toCharArray()strsc.next() ;
//fill code to get the words in the String
String[] words = sc.nextLine()namewordsname.split("
")name.toCharArray()strsc.next() ;
for(String sc.nextLine()namewordsname.split("
")name.toCharArray()strsc.next() : sc.nextLine()namewordsname.split("
")name.toCharArray()strsc.next() )
System.out.println(str);
}
}
Feedback
Your answer is partially correct.
You have correctly selected 3.
The correct answer is:
Fill the code to split the words in a String and print them
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
String name=[sc.nextLine()];
//fill code to get the words in the String
String[] words = [name.split(" ")];
for(String [str] : [words] )
System.out.println(str);
}
}
Question 9
Correct
Mark 1.00 out of 1.00
Flag question
Question text
You are provided with a code to find if a given string is a palindrome or
not.
The code is shuffled. Rearrange the code in the correct order.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
StringBuffer sb = new StringBuffer(str);
sb.reverse();
String reverse=sb.toString();
if(str.equalsIgnoreCase(reverse))
System.out.println(str+" is a Palindrome");
else
System.out.println(str+" is not a Palindrome");
}
Feedback
Your answer is correct.
Started on Sunday, 28 April 2024, 10:12 PM
State Finished
Completed on Sunday, 28 April 2024, 10:22 PM
Time taken 10 mins 23 secs
Marks 16.00/21.00
Grade 76.19 out of 100.00
Feedback Congratulations!!! You have passed by securing more than 70%
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Determine the output:
public class Test
{
public static void main(String[] args)
{
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for(int i = 0; i < x.length; i++)
System.out.print(y[i] + " ");
}
}
Select one:
a.
0000
b.
00
c.
12
d.
1234
Feedback
Array x is initialized with 4 values and this means reference "x" contains
the starting address of the array. This address is copied to the array
reference "y". This mean the 4 values can now be accessed with "y" as
well. Then the reference x is assigned with a new array's starting address
whose length is 2. Hence the iteration outputs 1 2
The correct answer is: 1 2
Question 2
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
class Output
{
public static void main(String args[])
{
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
System.out.println(a1.length + " " + a2.length);
}
}
Select one:
a.
0 10
b.
10 5
c.
05
d.
5 10
Feedback
Array a1 is created so as to contain 10 integer elements. Hence, the
length is 10.
Array a2 is initialized with 5 values. . Hence, the length is 5.
The correct answer is: 10 5
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Determine the output:
public class A
{
public static void main(String argv[])
{
int ary[]=new int[]{1,2,3};
System.out.println(ary[1]);
}
}
Select one:
a.
2
b.
1
c.
Compilation Error:incorrect syntax
Feedback
The array ary is initialized with 3 elements and the element at the first
index is 2.
The correct answer is: 2
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Determine the output
public class Trial
{
public static void main(String[] args)
{
int arr[4]={};
System.out.print(arr[0]);
}
}
Select one:
a.
Garbage error
b.
Compile time error
c.
Runtime error
d.
0
Feedback
int arr[4] is syntactically wrong
The correct answer is: Compile time error
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Determine the output:
public class Test
{
public static void main(String[] args)
{
int[] x = new int[3];
System.out.println("x[0] is " + x[0]);
}
}
Select one:
a.
The program has a runtime error because the array elements are not
initialized.
b.
The program runs fine and displays x[0] is 0.
c.
The program has a compile error because the size of the array wasn't
specified when declaring the array.
d.
The program has a runtime error because the array element x[0] is not
defined.
Feedback
The "new" keyword allows memory for storing integer elements in an
array to be created in the "heap" and the memory is initialized with
"default of integer" which is 0.
The correct answer is: The program runs fine and displays x[0] is 0.
Question 6
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Determine the output
class array_output
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
System.out.print(array_variable[i] + "");
}
}
}
Select one:
a.
iiiiiiiiii
b.
1 2 3 4 5 6 7 8 9 10
c.
ijklmnopqr
d.
0 1 2 3 4 5 6 7 8 9 10
Feedback
array_variable is a character array that can hold 10 characters. The for
loop gets iterated for 10 times. During each iteration, the array index is
assigned with the character "i" and printed alongside. Hence the output
"iiiiiiiiii".
The correct answer is: i i i i i i i i i i
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Determine the output:
class Evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.println(arr[n] / 2);
}
}
Select one:
a.
3
b.
1
c.
0
d.
6
Feedback
arr is an integer array that is initialized with 10 values.
When n is initialized with value 6, n = arr[arr[n] / 2] evaluates to n = 3.
Now, printing arr[n] / 2 will output 1.
Question text
What will be the content of array variable table after executing the
following code?
public class Trial
{
public static void main(String[] args)
{
int []table[]=new int[5][5];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(j == i)
{
table[i][j] = 1;
System.out.print(table[i][j]);
}
else
{
table[i][j] = 0;
System.out.print(table[i][j]);
}
}
System.out.println("\n");
}
}
}
Select one:
a.
100
110
111
b.
000
000
000
c.
Compilation error
d.
100
010
001
Feedback
"table" is a 2 dimensional array with 5 rows and 5 columns. It is iterated
from 0 through 3 and during each iteration j==i (iteration variables) is
checked. When j==i evaluates to true, the index is assigned the value "1"
and printed. Else, assigned "0'" and printed.
The correct answer is:
100
010
001
Question 9
Incorrect
Mark 0.00 out of 1.00
Flag question
Question text
Given a one dimensional array arr, what is the correct way of getting the
number of elements in arr is arr.length()-1 arr.length-
1 arr.length() arr.length
Feedback
Your answer is incorrect.
The correct answer is:
Given a one dimensional array arr, what is the correct way of getting the
number of elements in arr is [arr.length]
Question 10
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Column size is mandatory to create an array in java. State true or false
Select one:
True
False
Feedback
The correct answer is 'False'.
Question 11
Correct
Mark 1.00 out of 1.00
Flag question
Question text
new is used to allocate memory to array variable in Java
malloc calloc alloc
Feedback
Your answer is correct.
The correct answer is:
[new] is used to allocate memory to array variable in Java
Question 12
Correct
Mark 1.00 out of 1.00
Flag question
Question text
length() is used to find string length.
Question 13
Partially correct
Mark 0.33 out of 1.00
Flag question
Question text
Fill in appropriately.
String st1 = new String("JAVA");
String st2 = new String("JAVA");
String st3="JAVA"
321 objects, 231 in heap memory and 123 in string pool
Feedback
The "new" keyword allows memory for storing String to be allocated in the
"heap" . Otherwise, memory is allocated in string pool.
You have correctly selected 1.
The correct answer is:
Fill in appropriately.
String st1 = new String("JAVA");
String st2 = new String("JAVA");
String st3="JAVA"
[3] objects, [2] in heap memory and [1] in string pool
Question 14
Partially correct
Mark 0.67 out of 1.00
Flag question
Question text
What is special about string objects as compared to objects of other
derived types?
Question text
Given:
1. public class MyLogger {
2. private StringBuilder logger = new StringBuuilder();
3. public void log(String message, String user) {
4. logger.append(message);
5. logger.append(user);
6. }
7. }
The programmer must guarantee that a single MyLogger object works
properly for a multi-threaded system.
How must this code be changed to be thread-safe?
Select one:
a.
Replace StringBuilder with StringBuffer
b.
No change is necessary, the current MyLogger code is already thread-
safe.
c.
Synchronize the log method
d.
Replace StringBuilder with just a String object and use the string
concatenation (+=) within the log method.
Feedback
StringBuffer is synchronized and therefore thread-safe. StringBuilder is
compatible with StringBuffer API but with no guarantee of synchronization.
Because it’s not a thread-safe implementation, it is faster and it is
recommended to be used only in places where there’s no need for thread
safety.
The correct answer is: Replace StringBuilder with StringBuffer
Question 16
Correct
Mark 1.00 out of 1.00
Flag question
Question text
ello is the string contained in s after following lines of code?
StringBuffer s new StringBuffer(“Hello”);
s.deleteCharAt(0);
Question 17
Correct
Mark 1.00 out of 1.00
Flag question
Question text
String s3 = (String)'abc';String s4 = (String)'\ufeed'; String s1 =
null;String s2 = 'null'; is the valid declaration of a String.
Feedback
Your answer is correct.
The correct answer is:
[ String s1 = null;] is the valid declaration of a String.
Question 18
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Predict the output
class String_demo
{
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
}
}
Select one:
a.
b
b.
a
c.
c
d.
abc
Feedback
A character array is initialized with 'a', 'b' and 'c' and the array reference
is chars. Printing this reference will output abc.
A "new" string object is initialized with this reference and this object is
referred by "s". Printing this reference will output abc.
The correct answer is: abc
Question 19
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Predict the output
class String_demo
{
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
Select one:
a.
ABCD
b.
ABC
c.
BCD
d.
CDA
Feedback
An integer array is initialized with values 65, 66, 67 and 68. Its reference
is "ascii". A new string object is initialized with this reference such that
the elements from index 1 through 3 alone gets copied as "characters".
This object is referred by "s". Printing this object will output BCD which are
the char-equivalents of 66, 67 and 68.
The correct answer is: BCD
Question 20
Correct
Mark 1.00 out of 1.00
Flag question
Question text
+ operator can be used to concatenate two or more String objects in java.
State true or false.
Select one:
True
False
Feedback
The correct answer is 'True'.
Question 21
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What will s2 contain after following lines of code?
String s1 = “one”;
String s2 = s1.concat(“two”);
Select one:
a.
twoone
b.
onetwo
c.
two
d.
one
Feedback
The string "two" referred by s2 is "concatenated to" the string "one"
referred by s1.
Question text
class TestArray {
public static void main(String args[]) {
int arr_sample[] = new int[2];
System.out.println(arr_sample[0]);
}
}
What will be the result of compiling and executing the above code?
Select one:
a.
The program generates a runtime exception because arr_sample[0] is
being read before being initialized.
b.
The program compiles and runs but the results are not predictable
because of un-initialized memory being read.
c.
The program compiles and prints 1 when executed.
d.
The program compiles and prints 0 when executed.
e.
The program does not compile because arr_sample[0] is being read before
being initialized.
Feedback
Your answer is correct.
When an array is just declared with the size without providing any values,
it takes the default value.
The correct answer is: The program compiles and prints 0 when executed.
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
class ArrayTest {
public static void main(String args[]) {
int[] primes = new int[10];
primes[0] = "a";
System.out.println(primes[0]);
}
}
What will be the result of compiling and executing the above code?
Select one:
a.
a
b.
Compile time error
c.
Runtime exception
d.
ArrayStoreException
Feedback
Your answer is correct.
In an array of integer, we cannot store a String. Results in Type mismatch.
Hence compilation error.
The correct answer is: Compile time error
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
StringBuffer is used to create ______________.
Select one:
a.
a mutable String
b.
an immutable String
Feedback
Your answer is correct.
The correct answer is: a mutable String
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
String name="teknoturf";
String cname="teknoturf";
String compname=new String("teknoturf");
1.if(name==cname)
2.if(name.equals(cname))
3.if(name==compname)
4.if(name.equals(compname))
Identify the output.
Select one:
a.
Line 3,4 will return true.
b.
Line 1,3 will return true.
c.
Line 1,3,4 will return true.
d.
Line 1,2,4 will return true.
Feedback
Your answer is correct.
The correct answer is: Line 1,2,4 will return true.
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
In which of the following packages can you find String class?
Select one:
a.
util
b.
none of the options
c.
io
d.
lang
Feedback
Your answer is correct.
The correct answer is: lang
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
List the correct ways of declaring an Array.
Select one or more:
a.
int [ ]studentId;
b.
String name[]=new String(10);
c.
String [ ] name [ ];
d.
int studentId[10];
e.
int studentId[ ];
Feedback
Your answer is correct.
The correct answers are: int [ ]studentId;, int studentId[ ];, String [ ] name
[ ];
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
StringBuilder is less efficient and slower than StringBuffer. State true or
false.
Select one:
True
False
Feedback
The correct answer is 'False'.
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
State True or False.
Advanced for loop is better as it is less error prone as we don't need to
deal with index.
Select one:
True
False
Feedback
The correct answer is 'True'.
Question 9
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Given a one-dimensional array arr, what is the correct way of getting the
number of elements in arr?
Select one:
a.
arr.length
b.
arr.length()-1
c.
arr.length-1
d.
arr.length()
Feedback
Your answer is correct.
The correct answer is: arr.length
Question 10
Correct
Mark 1.00 out of 1.00
Flag question
Question text
String Objects are mutable. State true or false.
Select one:
True
False
Feedback
The correct answer is 'False'.
Question 11
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is the output of this program?
class Output {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
System.out.println(a1.length + " " + a2.length);
}
}
Select one:
a.
5 10
b.
05
c.
10 5
d.
0 10
Feedback
Your answer is correct.
The correct answer is: 10 5
Started on Sunday, 28 April 2024, 10:50 PM
State Finished
Completed on Sunday, 28 April 2024, 10:52 PM
Time taken 1 min 52 secs
Marks 3.00/3.00
Grade 100.00 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Select the correct choice so that the below statement returns true, if the
input provided contains any alphabet other than xyz.
Pattern.matches(" [a-z&&[^xyz]][a-z&[xyz]][a-z&&[xyz]][a-
z&[^xyz]] ", "d")
Feedback
Your answer is correct.
The correct answer is:
Select the correct choice so that the below statement returns true, if the
input provided contains any alphabet other than xyz.
Pattern.matches("[[a-z&&[^xyz]]]", "d")
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Assume that the ID of an employee should start with "CBE" or "BLR" or
"HYD" followed by hyphen (-) followed by 4 digits.
Choose the apt regular expression that matches this text.
Select one:
a.
(CBE/HYD/BLR)[-][0-9]{4}
b.
[CBE|HYD|BLR][-][0-9]{4}
c.
(CBE|HYD|BLR)[-][0-9]{4}
d.
[CBE|HYD|BLR][-][0-9]*
e.
[CBE/HYD/BLR][-][0-9]*
Feedback
Your answer is correct.
The correct answer is: (CBE|HYD|BLR)[-][0-9]{4}
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Which of the following matches X occurs n or more times?
Select one:
a.
X{n,…}
b.
X{n,}
c.
X{n,*}
d.
X{n}
Feedback
Your answer is correct.
The correct answer is: X{n,}
Started on Sunday, 28 April 2024, 10:52 PM
State Finished
Completed on Sunday, 28 April 2024, 10:55 PM
Time taken 2 mins 43 secs
Marks 7.00/8.00
Grade 87.50 out of 100.00
Question 1
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Predict the output.
import java.util.regex.*;
public class Main {
public static void main(String[]args)
{
String s="India";
String r="\\";
s=s.replaceAll(r,"a");
System.out.println(s);
}
}
Select one:
India
Indi
PatternSyntaxException
a
Feedback
Your answer is correct.
The correct answer is: PatternSyntaxException
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What does public boolean lookingAt() method do ?
Select one:
It matches the entire region against the pattern
It returns the offset after the last character matched.
It matches the input sequence at the beginning of the region against the
pattern
It returns the start index of the previous match
Feedback
Your answer is correct.
The correct answer is: It matches the input sequence at the beginning of
the region against the pattern
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Predict the output.
import java.util.regex.*;
public class Main {
public static void main(String[]args)
{
Pattern p=Pattern.compile("\\d");
String test="India123";
Matcher m=p.matcher(test);
if(m!=null)
{
System.out.println(m.find());
System.out.println(m.matches());
}
}
}
Select one:
false, false
true, false
true, true
false, true
Feedback
Your answer is correct.
The correct answer is: true, false
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is the Regex for below String?
The string should consist of only lowercase and uppercase letters (no
numbers or symbols).
The string should end in s.
Select one:
^((?i)[A-Z]*s)
^([(?i)a-z]*S)
([a-z]*s)$
^([a-zA-Z]*s)$
Feedback
Your answer is correct.
The correct answer is: ^([a-zA-Z]*s)$
Question 5
Not answered
Marked out of 1.00
Flag question
Question text
Predict the output.
import java.util.regex.*;
public class Main {
public static void main(String[]args)
{
String s="REGULAREXPRESSION";
String r="";
s=s.replaceAll(r,",");
System.out.println(s);
}
}
Select one:
REGULAR,EXPRESSION
REGULAREXPRESSION
REGULAREXPRESSION
,R,E,G,U,L,A,R,E,X,P,R,E,S,S,I,O,N,
Feedback
Your answer is incorrect.
The correct answer is: ,R,E,G,U,L,A,R,E,X,P,R,E,S,S,I,O,N,
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Which of the following about n+ is correct?
Select one:
Matches a string that contains zero n only
Matches a string that contains more than one n
Matches a string that contains one n only
Matches a string that contains at least one n
Feedback
Your answer is correct.
The correct answer is: Matches a string that contains at least one n
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Predict the output of the program.
public class Ex {
public static void main(String[] args) {
String cid = "2354126772";
String rgx1 = "[1-9]{10}";
if (cid.matches(rgx1) == true)
System.out.println("Valid customer");
else
System.out.println("Invalid customer");
}
}
Select one:
runtime exception
compile time error
Valid customer
Invalid customer
Feedback
Your answer is correct.
The correct answer is: Valid customer
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is the use of \w in regex?
Select one:
Used for a non-word character
Used for a word character
Used for a non-whitespace character
Used for a whitespace character
Feedback
Your answer is correct.
The correct answer is: Used for a word character