Complete Subject Question Bank
Using Java we can develop ___________________.
Correct answer: Both the options.
The main method in java should ___________.
Correct answer: be public static.
The break statement cannot be present for _____________ construct.
Correct answer: if.
A continue statement makes the execution jump to ______________.
Correct answer: the next iteration of the loop.
Identify the features of java.
Correct answer: Exception Handling.
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?
Correct answer: French curly braces{ }.
JRE comprises of ___________ and ___________.
Correct answer: JVM; API.
1What 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 "); } } } }
Correct answer: a-2 a-1 a default a-1 default a-1.
The ________________ statement causes the program execution to stop and JVM to shut down.
Correct answer: System.exit.
JVM is independent of OS
JVM abstracts the OS; Java bytecode runs on any JVM regardless of OS.
To compile, debug and execute a program written in java, _______________ is required.
Correct answer: JDK.
State true or false. Java is a structured programming language.
Correct answer: Java is a structured programming languag.
Who is the father of Java?
Correct answer: James Gosling.
What is Polymorphism?
Correct answer: ability to have many forms.
How was Java initially named?
Correct answer: The Oak.
Java is _____________________________.
Correct answer: Platform independent.
1What is the output of this program? 1. class Crivitch { 2. public static void main(String [] args) { 3. int x = 10; 4. 5. do { } while (x++ < y); 6. System.out.println(x); 7. } 8. } Which statement, inserted at line 4, produces the output 12?
Correct answer: int y=11;.
1What will be the output of the program? Given: 10. int x = 0; 11. int y = 10; 12. do { 13. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + "," + y); What is the result?
Correct answer: 5,5.
1What is the output of this program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } }
Correct answer: 2.
1Fill in the appropriate data type for the Java switch statement: switch (____) { case value1: ... case value2: ... default: System.out.println("Hello"); }
Java switch supports byte, short, char, int, their wrapper classes, enums, and String. Among the options, byte is valid.
What value is stored in i at the end of this loop? for(int i =1;i<=10;i++)
Correct answer: 11.
The break statement causes an exit ___________
Correct answer: from the innermost loop; from the innermost switch..
Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
Correct answer: do-while.
1French curly braces { } is a must if the for loop executes more than one statement. State true or false.
Correct answer: TRUE.
1What will be the output of the program? for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done");
Correct answer: one two three two three done.
1What will be the output of the program? int i = 1, j = -1; switch (i) { case 0, 1: j = 1; /* Line 4 */ case 2: j = 2; default: j = 0; } System.out.println("j = " + j);
Correct answer: Compilation fails..
A project developed on one machine can be included in the current workspace by ___________ that project.
Correct answer: Importing.
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.
Correct answer: True.
We can move an already existing project in eclipse to another location by compressing it. This we call as ________ the project.
Correct answer: Exporting.
Eclipse, the plugin that is needed for Java Development is ______________.
Correct answer: JDT.
Predict the output int a=0; if(a) System.out.println( "Hello"); else System.out.println( "Hai");
Correct answer: Compilation Fails.
1What 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 ) ; } }
Correct answer: No output is produce.
__________ generates the byte code for a given file with .java extension.
Correct answer: JDK.
Which edition of java is used for developing web application?
Correct answer: J2EE.
State True or False For compiling a java code we have separate compilers for different OS.
Eclipse IDE creates a new workspace directory automatically if it does not exist.
Which of the following options remain true for case constants in a switch construct?
Correct answer: 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.
Who executes the byte code in java?
Correct answer: JVM.
The methods of a class with the ____________ access specifier cannot be accessed in its subclass class of different package.
Correct answer: default.
1Predict the output. class X { void display(int a) { System.out.println("INT"); } void display(double d) { System.out.println("DOUBLE"); } } public class Sample { public static void main(String[] args) { new X().display(100); } }
Correct answer: INT.
1Observe the code public class Sample { public static void main(String [] args) { int x = 6; Sample p = new Sample(); p.display(x); System.out.print(" main x = " + x); } void display(int x) { System.out.print(" display x = " + x++); } } Given in command line - java Sample - What is the result?
Correct answer: display x = 6 main x = 7 f. display x = 6 main x = 6.
Integer x1 = new Integer(120); int x2 = 120; System.out.println( x1 == x2 ); What will be the output of the above code fragment?
Correct answer: true.
1Given: 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 ?
Correct answer: Compilation fails.
1Given 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. } Identify the statement to be written in line 5 in class TestMain to call the display method of class Test.
Correct answer: p1.Test.display(names);.
1What is the outcome of the code? public class Item { private String description; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public static void modifyDesc(Item item,String desc) { item=new Item(); item.setDescription(desc); } public static void main(String[] args) { Item it=new Item(); it.setDescription("Gobstopper"); Item it2=new Item(); it2.setDescription("Fizzylifting"); it.modifyDesc(it,"Scrumdiddlyumptious"); System.out.println(it.getDescription()); System.out.println(it2.getDescription()); } }
Correct answer: Gobstopper Fizzylifting.
Which of the following is not a Java modifier?
Correct answer: virtual.
1switch(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
Correct answer: 2,3, 4 and 6.
1Predict the output. public class Test { public static void main(String args[]) { int a = 2, b = 0; for ( ; b < 20; ++b) { if (b % a == 0) continue; else if (b == 10) break; else System.out.print(b + " "); } } }
The loop skips even values using continue. The break condition b == 10 is never reached because 10 is even and continue executes first, so all odd values from 1 to 19 are printed.
1What 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; }
Correct answer: Compilation fails.
State True or False When using eclipse whichever classes are needed for the present class can be imported automatically.
Eclipse can auto-import required classes using Ctrl+Shift+O or Quick Fix.
Eclipse IDE, if we provide a workspace, it should already exist. If not, it will not open.
Eclipse IDE will create the workspace folder if it does not already exist.
___ and _____ are the access specifiers that can be applied to top level Class.
Correct answer: default; public.
1class 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)); } }
Correct answer: Compile time error.
1Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere. class Test { public int value; }
Correct answer: class Test { [public] int value; }.
1Consider 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); } }
studentId is private; accessing it from Test directly causes a compile-time error.
1The 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.
The method getId() is not defined in Book; getBookId() should be used instead.
1Observe the below code. public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2
Correct answer: public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2.
1Observe the code below. public class Student { int studentId; String name; char grade; public Student(int studentId, String name, float mark) { this.studentId = studentId; this.name=name; calculateGrade(mark); } public void calculateGrade(float mark){ if(mark>90) grade='A'; else grade='B'; } } For the code Student s = new Student(1,"Peter",95); What will be the output?
The constructor runs and grade is set to A but no print statement exists in the given code.
1Observe the below class. class Product{ int productId; String productName; Product { productId=0; productName= ; } Product(int id, String name) { //access Product() ---- Line 1 productId=id; productName=name; } } Identify the valid option which is used to invoke the no argument constructor, Product(), at Line 1.
this() calls the no-argument constructor of the same class within a constructor.
1Given 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?
The countBook method is private; it cannot be accessed from the Library class in another package.
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.
From package p2, Book can be accessed as p1.Book directly, or via import statements.
1Assume 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?
Only public allows access from a different package without inheritance.
1Choose the correct order of the Java code fragments: 1) public class Main, 2) import java.util.Scanner;, 3) {, 4) // Some code here, 5) }, 6) package test;
In Java, the package declaration comes first, followed by imports, then the class declaration and class body.
1For 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 } }
A static method can be called using the class name (Test.display()) or via an instance.
Given a one-dimensional array arr, what is the correct way of getting the number of elements in arr?
Correct answer: arr.length().
1class 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?
Correct answer: compile time error.
Partially List the correct ways of declaring an Array.
Correct answer: String [ ] name [ ];; int studentId[ ];; int [ ]studentId;.
1class 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?
Correct answer: The program compiles and prints 0 when execute.
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.
Correct answer: Line 1,2,4 will return tru.
1What 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); } }
Correct answer: 10 5.
StringBuilder is less eficient and slower than StringBuffer. State true or false.
Correct answer: TRUE.
State True or False. Advanced for loop is better as it is less error prone as we don't need to deal with index.
Correct answer: TRUE.
which of the following packages can you find String class?
Correct answer: lang.
StringBuffer is used to create ______________.
Correct answer: a mutable String.
String Objects are mutable. State true or false.
Correct answer: State true or fals.
What does this() mean in constructor chaining concept?
Correct answer: Used for calling the no argument constructor of the same class..
1package edu.ABC.model; public class Account { public static final float INTERTEST_RATE = 7.5; } Identify the correct options from the classes provided below.
Correct answer: import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTER5EST2_RAT2E; 48 } }; import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } }.
Identify which statement is true about construtors.
Correct answer: Constructor can be overloaded.
Which members of a class can be accessed by other classes is determined by the ________________
Correct answer: Access specifier.
1Predict the Output of following Java Program. class Test { int x = 10; public static void main(String[] args) { System.out.println(x); } }
Correct answer: Compile Time Error.
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
Correct answer: 1 and 2.
1Given: 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?
Correct answer: CompileTime Error.
A JavaBeans component has the following field: private boolean enabled; Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.)
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 first name of a person should contain only alphabets and space. Which of the following regular expression will match the requirement?
Correct answer: [a-zA-Z ].
Which of the following text when matched with the regular expression [a-zA-Z&&[^aeiou]]+ will return true?
Correct answer: My; cry.
What is the regular expression to match a whitespace character in a string?
Array index size is not negative; negative elements can be stored in an array.
What is the regular expression to match a digit (0-9) in a string?
\d matches any digit character (0–9) in Java regex.
00means X occurs zero or more times
In Java regex, * quantifier means zero or more occurrences.
What is the regular expression to match any email address in a string?
A comprehensive email regex checks local part, @ symbol, domain, and TLD.
Consider the below statements. Statement 1 : Matcher class interprets the pattern in a String Statement 2 : Matcher class matches the regular expression against the text provided Which of the following is true?
Correct answer: Both Statement 1 and 2 are correct.
\B means A word boundary
\B (uppercase) matches a non-word boundary position; \b (lowercase) matches a word boundary.
1Predict the output of the below code : import java.util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); } }
Correct answer: true.
What is the regular expression to match any date in the format "yyyy-mm-dd" in a string?
The pattern \d{4}-\d{2}-\d{2} matches yyyy-mm-dd format dates.
Observe the below code snippet String name="Sudha learns Oracle"; System.out.println(name.substring(7,12)); What is the output of the above code?
Correct answer: learns.
Predict the output of the below code : String emailId="john#global.com"; System.out.println(emailId.indexOf('@'));
Correct answer: -1.
What can be the parameters for the indexOf method in String class?
Correct answer: int; String.
Observe the below code : String course="Java Programming"; char c=course.charAt(16); System.out.println(c); What will be the output for the above code snippet?
Correct answer: g.
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.
The regex (CBE|BLR|HYD)-\d{4} matches the required employee ID format.
Which of the following matches X occurs n or more times?
X{n,} matches X repeated n or more times in Java regex.
The ________________ statement causes the program execution to stop and JVM to shut down.
Correct answer: exit.
Using Java we can develop ___________________.
Correct answer: Both the options.
The break statement cannot be present for _____________ construct.
Correct answer: if.
A continue statement makes the execution jump to ______________.
Correct answer: the next iteration of the loop.
1What 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 "); } } } } }
Correct answer: a-2 a-1 a default a-1.
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?
Correct answer: French curly braces{ }.
Identify the features of java.
Correct answer: Multi threading.
JRE comprises of ___________ and ___________.
Correct answer: API; JVM.
The main method in java should ___________.
Correct answer: be public static.
Java is _____________________________.
Correct answer: Platform independent.
JVM is independent of OS
JVM is platform-independent; it abstracts the underlying OS.
To compile, debug and execute a program written in java, _______________ is required.
Correct answer: JDK.
How was Java initially named?
Correct answer: The Oak.
State true or false. Java is a structured programming language.
Correct answer: Java is a structured programming languag.
Who is the father of Java?
Correct answer: James Gosling.
What is Polymorphism?
Correct answer: ability to have many forms.
The break statement causes an exit ___________
Correct answer: from the innermost switch.; from the innermost loop.
1What is the output of this program? 1. class Crivitch { 2. public static void main(String [] args) { 3. int x = 10; 4. 5. do { } while (x++ < y); 6. System.out.println(x); 7. } 8. } Which statement, inserted at line 4, produces the output 12?
Correct answer: int y=11;.
1Fill in the appropriate data type for the Java switch statement: switch (____) { case value1: ... case value2: ... default: System.out.println("Hello"); }
Java switch supports byte, short, char, int, their wrapper classes, enums, and String. Among the options, byte is valid.
What value is stored in i at the end of this loop? for(int i =1;i<=10;i++)
Correct answer: 11.
1What is the output of this program? class selection_statements { public static void main(String args[]) { int var1 = 5; int var2 = 6; if ((var2 = 1) == var1) System.out.print(var2); else System.out.print(++var2); } }
Correct answer: 2.
1What will happen when the following code is compiled and run in Java 1.8? int i = 1, j = -1; switch(i) switch (i) { case 0, 1: j = 1; /* Line 4 */ case 2: j = 2; default: j = 0; } System.out.println("j = " + j);
Correct answer: Compilation fails. One can not specify multiple case labels with commas, as in line 4. Hence compilation error..
1What will be the output of the program? for(int i = 0; i < 3; i++) { switch(i) { case 0: break; case 1: System.out.print("one "); case 2: System.out.print("two "); case 3: System.out.print("three "); } } System.out.println("done");
Correct answer: done.
1What will be the output of the program? Given: 10. int x = 0; 11. int y = 10; 12. do { 13. y--; 14. ++x; 15. } while (x < 5); 16. System.out.print(x + "," + y); What is the result?
Correct answer: 5,5.
1French curly braces { } is a must if the for loop executes more than one statement. State true or false.
Correct answer: TRUE.
Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
Correct answer: do-while.
State True or False. When typing the code in code editor, it shows the compilation error. It also assists in how to fix that error.
Correct answer: True.
Eclipse, the plugin that is needed for Java Development is ______________.
Correct answer: JDT.
A project developed on one machine can be included in the current workspace by ___________ that project.
Correct answer: Importing.
We can move an already existing project in eclipse to another location by compressing it. This we call as ________ the project.
Correct answer: Exporting.
Which of the following options remain true for case constants in a switch construct?
Correct answer: The code with the switch construct gives a compilation error when there is a duplicate case label.
1What 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); while (i > 10); } }
Correct answer: No output is produce.
State True or False For compiling a java code we have separate compilers for different OS.
Java uses a single platform-independent compiler (javac); it compiles to bytecode, not OS-specific code.
__________ generates the byte code for a given file with .java extension.
Correct answer: JDK.
Who executes the byte code in java?
Correct answer: JVM.
Which edition of java is used for developing web application?
Correct answer: J2EE.
Predict the output int a=0; if(a) System.out.println( "Hello"); else System.out.println( "Hai");
Correct answer: Compilation Fails.
Which of the following is not a Java modifier?
Correct answer: virtual.
1Predict the output. class X { void display(int a) { System.out.println("INT"); } void display(double d) { System.out.println("DOUBLE"); } } public class Sample { public static void main(String[] args) { new X().display(100); } }
Correct answer: INT.
Integer x1 = new Integer(120); int x2 = 120; System.out.println( x1 == x2 ); What will be the output of the above code fragment?
Correct answer: true.
1Observe the code public class Sample { public static void main(String [] args) { int x = 6; Sample p = new Sample(); p.display(x); System.out.print(" main x = " + x); } voiddisplay(intx){ void display(int x) { System.out.print(" display x = " + x++); } } Given in command line - java Sample - What is the result?
Correct answer: display x = 6 main x = 6.
1Given 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. } Identify the statement to be written in line 5 in class TestMain to call the display method of class Test.
Correct answer: p1.Test.display(names);.
The methods of a class with the ____________ access specifier cannot be accessed in its subclass class of different package.
Correct answer: default.
1What is the outcome of the code? public class Item { private String description; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public static void modifyDesc(Item item,String desc) { item=new Item(); item.setDescription(desc); } public static void main(String[] args) { Item it=new Item(); it.setDescription("Gobstopper"); Item it2=new Item(); it2.setDescription("Fizzylifting"); it.modifyDesc(it,"Scrumdiddlyumptious"); System.out.println(it.getDescription()); System.out.println(it2.getDescription()); } }
Correct answer: Gobstopper Fizzylifting.
1Given: 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 ?
Correct answer: Compilation fails.
1Predict the output. public class Test { public static void main(String args[]) { int a = 2, b = 0; for ( ; b < 20; ++b) { if (b % a == 0) continue; else if (b == 10) break; else System.out.print(b + " "); } } }
The loop skips even values using continue. The break condition b == 10 is never reached because 10 is even and continue executes first, so all odd values from 1 to 19 are printed.
Eclipse IDE, if we provide a workspace, it should already exist. If not, it will not open.
Eclipse IDE creates a new workspace automatically if the specified path does not exist.
1What 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; }
Correct answer: Compilation fails.
1switch(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
Correct answer: 2,3, 4 and 6.
State True or False When using eclipse whichever classes are needed for the present class can be imported automatically.
Eclipse can automatically add missing imports via Quick Fix or Organize Imports.
___ and _____ are the access specifiers that can be applied to top level Class.
Correct answer: public; default.
1class 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)); } }
Correct answer: Compile time error.
1Choose the appropriate access specifier for the attribute value so that it can be accessed from anywhere. class Test { public int value; }
Correct answer: class Test { [public] int value; }.
1Consider 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); } }
studentId is private; accessing it from outside the class causes a compile-time error.
1The 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.
The error is that getId() is not defined; the correct method is getBookId().
1Observe the below code. public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2
Correct answer: public class Student { private int id; private String name; private char grade; //Constructor 1 public Student() { id=0; name= " "; } //Constructor 2 public Student(int id, String name) { this.id=id; this.name=name; } } Choose the constructor that is invoked, when an object is created as shown below. new Student(); Constructor 1 new Student(54, "John"); Constructor 2.
1Observe the code below. public class Student { int studentId; String name; char grade; public Student(int studentId, String name, float mark) { this.studentId = studentId; this.name=name; calculateGrade(mark); } public void calculateGrade(float mark){ if(mark>90) grade='A'; else grade='B'; } } For the code Student s = new Student(1,"Peter",95); What will be the output?
The code runs without error but includes no print statement, so no output is produced.
1Observe the below class. class Product{ int productId; String productName; Product { productId=0; productName= ; } Product(int id, String name) { //access Product() ---- Line 1 productId=id; productName=name; } } Identify the valid option which is used to invoke the no argument constructor, Product(), at Line 1.
this() is used within a constructor to call another constructor of the same class.
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.
From package p2, Book can be accessed as p1.Book directly, or via import statements.
1Given 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?
countBook is private; it cannot be accessed from Library in a different package.
1Choose the correct order of the Java code fragments: 1) public class Main, 2) import java.util.Scanner;, 3) {, 4) // Some code here, 5) }, 6) package test;
In Java, the package declaration comes first, followed by imports, then the class declaration and class body.
1Assume 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?
Only public allows cross-package static method access from CalculatorService.
1For 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 } }
Static methods can be invoked via class name or via an instance reference.
List the correct ways of declaring an Array.
Correct answer: int [ ]studentId;; String [ ] name [ ];; int studentId[ ];.
1What 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); } }
Correct answer: 0 5.
StringBuffer is used to create ______________.
Correct answer: a mutable String.
Given a one-dimensional array arr, what is the correct way of getting the number of elements in arr?
Correct answer: arr.length-1.
1class 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?
Correct answer: compile time error.
State True or False. Advanced for loop is better as it is less error prone as we don't need to deal with index.
Correct answer: TRUE.
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.
Correct answer: Line 1,2,4 will return tru.
String Objects are mutable. State true or false.
Correct answer: State true or fals.
1class 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?
Correct answer: The program compiles and prints 1 when execute.
which of the following packages can you find String class?
Correct answer: lang.
StringBuilder is less efficient and slower than StringBuffer. State true or false.
Correct answer: TRUE.
1Given: 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?
Correct answer: CompileTime Error.
1Predict the Output of following Java Program. class Test { int x = 10; public static void main(String[] args) { System.out.println(x); } }
Correct answer: Compile Time Error.
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
Correct answer: 1 and 2.
A JavaBeans component has the following field: private boolean enabled; Which two pairs of method declarations follow the JavaBeans standard for accessing this field? (Choose two.)
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.
1package edu.ABC.model; public class Account { public static final float INTERTEST_RATE = 7.5; } Identify the correct options from the classes provided below.
Correct answer: import edu.ABC.model.Account ; public class Loan { public double getInterest() { return Account.INTEREST_RATE; } }; import static edu.ABC.model.Account.*; public class Loan { public double getInterest() { return INTEREST_RATE; } }.
Which members of a class can be accessed by other classes is determined by the ________________
Correct answer: Access specifier.
Identify which statement is true about construtors.
Correct answer: Constructor can be overloaded.
What does this() mean in constructor chaining concept?
Correct answer: Used for calling the no argument constructor of the same class..
1class 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); } }
Correct answer: 10 5.
_______________ is used to allocate memory to array variable in Java
Correct answer: new.
1Predict the output p 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); } }
Correct answer: ABCD.
1Determine 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); } }
Correct answer: 1.
1Predict 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); } }
Correct answer: a.
1Determine the output: (MCQ) public class Test { public static void main(String[] args) { int[] x = new int[3]; System.out.println("x[0]is"+x[0]); Sys e .ou .p ( [ ] s [ ]); } }
Correct answer: The program runs fine and displays x[0] is 0. 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..
1Given: 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?
Correct answer: Replace StringBuilder with StringBuffer.
________________ is used to find string length.
Correct answer: length().
1Determine 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] + " "); } }
Correct answer: 1 2.
What is special about string objects as compared to objects of other derived types?
Correct answer: You can create string objects without or without using new operator; Java provides string constant pool to store the string objects; You can concatenate two string objects using +.
__________________ is the string contained in s after following lines of code? StringBuffer s = new StringBuffer(Hello); s.deleteCharAt(0);
Correct answer: llo.
1What 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"); } } }
The two-dimensional int array declaration is valid. The nested loop sets diagonal cells to 1 and other visited cells to 0, printing a 3x3 identity pattern.
What will s2 contain after following lines of code? String s1 = one; String s2 = s1.concat(two);
Correct answer: one.
1Determine 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] + ""); } } }
Correct answer: i i i i i i i i i i.
+ operator can be used to concatenate two or more String objects in java. State true or false.
Correct answer: State true or fals.
Column size is mandatory to create an array in java. State true or false
Correct answer: TRUE.
1Determine the output public class Trial { public static void main(String[] args) { int arr[4]={}; System.out.print(arr[0]); } }
Correct answer: Compile time error.
1Determine the output: public class A { public static void main(String argv[]) { int ary[]=new int[]{1,2,3}; System.out.println(ary[1]); } }
Correct answer: 2.
Which of the following text when matched with the regular expression [a-zA-Z&&[^aeiou]]+ will return true?
Correct answer: My; cry.
What is the regular expression to match any date in the format "yyyy-mm-dd" in a string?
The regex \d{4}-\d{2}-\d{2} matches dates in yyyy-mm-dd format.
a regular expression, the pattern 00 means, 0 occurs exactly two times.
In regex "00" means the pattern "0" appears exactly two times.
What is the regular expression to match a whitespace character in a string?
\s matches any whitespace character.
The first name of a person should contain only alphabets and space. Which of the following regular expression will match the requirement?
Correct answer: [a-zA-Z ]+.
What is the regular expression to match any email address in a string?
An email regex like [a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+ matches email addresses.
Consider the below statements. Statement 1 : Matcher class interprets the pattern in a String Statement2:Matcherclassmatchestheregularexpression againstthetextprovided Statement 2 : Matcher class matches the regular expression against the text provided Which of the following is true?
Correct answer: Both Statement 1 and 2 are correct.
What is the regular expression to match a digit (0-9) in a string?
\B matches a non-word boundary position, not a word boundary (that is \b).
1Predict the output of the below code : import java.util.regex.*; public class TestRegEx{ public static void main(String args[]) { Pattern p = Pattern.compile(".ech"); Matcher m = p.matcher("tech"); boolean b = m.matches(); System.out.println(b); } }
Correct answer: true.
\B means A word boundary
\B in regex means NON-word boundary; \b (lowercase) means word boundary.
Observe the below code snippet String name="Sudha learns Oracle"; System.out.println(name.substring(7,12)); What is the output of the above code?
Correct answer: learns.
Predict the output of the below code : String emailId="john#global.com"; System.out.println(emailId.indexOf('@'));
Correct answer: -1.
What can be the parameters for the indexOf method in String class?
Correct answer: String; int.
Observe the below code : String course="Java Programming"; char c=course.charAt(16); System.out.println(c); What will be the output for the above code snippet?
Correct answer: StringIndexOutOfBoundsException.
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.
The regex (CBE|BLR|HYD)-\d{4} matches IDs like CBE-1234.
Which of the following matches X occurs n or more times?
X{n,} matches X repeated n or more times.
Based on our question bank analysis, master these concepts to score high in Java Programming.
"Focus on understanding the logic behind pseudocode loops and selection statements, as they form the bulk of technical assessments."