You are on page 1of 20
Java Questions Latest Java MCQ Objective Questions & Ee ane TU Racal} Cie Re ctr See a cuba ict a) aoe ered ROS ree eucrnd B Download App MCQ Question 1: Which of the following statements is/are true regarding JAVA? A. Constants that cannot be changed are declared using the ‘Static’ keyword, B. Aclass can only inherit one class that can implement multiple interfaces. 1. Aistrue 2. Bistrue 3 z. and (8) are true 4. Neither (A) nor (B) is true Certo] India's Super Teachers for all govt. exams Leite mela oles ‘Answer (Detailed Solution Below) Option 2: Bis true Java MCQ Question 1 Detailed Solution Correct Answer: Option 2 Explanation: ~ + Constants that cannot be changed are declared using the final keyword. The static keyword is used for functions and variables to specify that they will be instantiated exactly once for any number of object instances and the same value will be used across all objects. + Java does not support multiple inheritances directly. When a class inherits two different classes, it is known as multiple inheritances. Java does not allow for a class to inherit more than one class, However, two classes can be inherited if one of them is an interface. An interface is a schematic representation of a class containing variables and abstract methods. So, a class can inherit one class, that can implement multiple interfaces. Therefore, statement A is incorrect and statement B Is correct. ee ean por aa) Start Complete Exam Preparation Cee Dan oe ost Pr or arsed ea aces Brest Crees rag peer Download App MCQ Question 2: View this Question Online > Which of the following methods will create a string in Java? |: String $ = "Hello Java”, Il: String $2 = new String (‘Hello Java"); 1. Only! . 2. Only 3. A... 4. Neither! nor Il ‘Answer (Detailed Solution Below) Option 3: Both | and I! Java MCQ Question 2 Detailed Solution The correct answer is option 3. Concept: Astring is an object in Java that indicates a collection of characters or char values, A Java string object is created using the java.lang.String class. AString object can be created in one of two ways: it By string literal: Java String literal is created by using doublé-quotes. Example: ~a String s="Welcome", By new keyword: Java String is created by using the keyword “new’. Example: String s=new String("Welcome"); Hence the correct answer is Both | and II Og Treen ar er c) SS Ela eLey yi) Cl) Lelie) Ree Dar ers ac fees Uiorncy Question Bank Download App MCQ Question 3: View this Question Online > a: is an API to develop cal User Interface (GUI) or Windows-based applications in Java. 2 1, Java Abstract Window 2. Java Reg a 3. \ 4. Java l/O Answer (Detailed Solution Below) Option 1 : Java Abstract Window Toolkit Java MCQ Question 3 Detailed Solution The correct answer is option 1. Concept: Java Abstract Window Toolkit: The Java AWT (Abstract Window Toolkit) API is us we a graphical user interface (GUI) or 0. windows-based Java applications. It is a set of gram interfaces (APIs) that Java programmers use to construct GUI ns tons, scroll bars, and windows. Sun Microsystems, the company that inven eated AWT as part of the Java Foundation Classes (FC). Hence the correct answer is ‘tract Window Toolkit. ©: Additional Information + A Java Applet is a sort of application that generates dynamic information by being embedded in a webpagg, It is a client-side application that runs within the browser. + The Java Regex or Regular Expression is an AP! to define a pattern for searching or manipulating strings. + Java I/O (Input and Output) is used to process the input and produce the output. Eo eee arco Roe) Start Complete Exam Preparation aces a ma Mi Eilers Question Bank Download App Exon) MCQ Question 4: View this Question Oniine >. fh What will the result of the following code: public class Main t publ { ints = Qt: b= tic void main(String args[]) int num =a / bj System.out..printIn(nun) ; } } 1. Output: The value of num is 10.5 2. Runtime error 3. Compilation error 4. None of these Answer (Detailed Solution Below) Option 3 : Compilation error Java MCQ Question 4 Detailed Solution The correct answer is option 3. ~ © Key Points Cea ce stcnS * The Main is declared where the main function is declared inside it. + The integer variable a is initialized with value 21, and this statement is terminated with a semicolon. + The variable b is initialized but not declared in the statement. + One more variable num is declared and initialized by an expression ( a/b ). + System.out.printin is sued to print the variable num. + Every variable need to be declared or defined with a particular detatype before its initialization + Hence the given code throws a compilation error. eee ee React Start Complete Exam Preparation iG Pras Pm aace 3 Chess DOS iene cores Download App MCQ Question 5: View this Question Online Exotiraty ‘Whats the output of this program? clase ancresen’ {4 gm Lae public static void main(String args{ ) te gp za ‘System.out.print(++g * @); Answer (Detailed Solution Below) Option 3: 32 Java MCQ Question 5 Detailed Solution — This question is based upon PRBaccnce of operators in java.So, take a look over the "precedence of operators’ list first on jave. Here, ++ have higher priority than *. ++q is called as the pre-increment operator in java which will just increase the g value by 1. By solving the equation will be reduced to print(4*g) as, g value is 8 (given in the question), So, answer turns out to be 32 here. ‘Top Java MCQ Objective Questi al eee ate eres) 8 te Start Complete Exam Preparation OR ues aC a Pcie Corsica. Pato Download App ieee fore MCQ Question 6 ts View this Question Online Whatis the use of javac’ command? 1. Execute a java program 2 _ program 3. Interpret ajava program 4. Compile a java program Answer (Detailed Solution Below) Option 4: Compile a java program Java MCQ Question 6 Detailed Solution Concept “Th [evaeicoirinand th dave commplga ayarae hema Mn a Gonimane peor. It reads a Java source proggaia from a textfile and creates a compiled Java class file. — ‘~ Javac filename [options] For example, to compile a program named Abc java, use this command: javac Abe java Ra ee India’s #1 Learning Platform Start Complete Exam Preparation CBee DR toes Question Bank Pita Download App Ca Dally Live Practice ie Mock Tests MCQ Question 7 View this Question Online Consider the following Java code fragment. Which of the following statement is true? Code Statement public class While 2 { 3 public void loop() 4 { 5 int x= 0; 6 while (1) 7 { 8 Spore printin("x plus one is" + (x + 9 } 10 } ni) np . There is syntax error in line no, 1 . There are syntax errors in line nos. 1 & 6 . There is syntax error in line no. 8 . There is syntax error in line no. 6 ‘Anewer (Detailed Solution Below) Option 4: There is syntax error in line no. 6 Java MCQ Question 7 Detailed Solution The correct answer is “option 4”. : EXPLANATION: ~a Option 1: FALSE q “While” is not a keyword, hence it is valid to use it as a class name. Option 2: FALSE ‘Since Option 1 is false, so this option is by default false. Option 3: FALSE Any string operation can contain an equation type of expression. So, this is not any kind of syntax error. Option 4: TRUE Java uses condition as Boolean expressions like: while(true) or while(alse) Hence, while(1) is the wrong syntax for java. while(1) will give compiler time error as it treats as type mismatch to convert from integer to boolean value. Hence, the correct answer is “option 4”, A Mistake Points Here Class name is While used which is not a keyword. while is a keyword in java but not While. So, we can use it as class name, Pg SE ABS bral) SS la merle Clue ele) rears faetcd Desire Question Bank Download App Sato, MCQ Question 8 Question Online > Which of the following is/are the rules to declare variables in Java? 1. Java keywords cannot be used as variable names 2. Allof the options 3. ~ afe case-sensitive 4, The first character must be a letter Answer (Detailed Solution Below) Option 2: All of the options Java MCQ Question 8 Detailed Solution The variable is the basic unit of storage in a Java program. A veriable is defined by the combination of an identifier, a type, and an optional initializer. r Variable names cannot be keyword, it is case- sensitive and the first character must be a letter, In Java, all variables must be declared before they can be used The basic form of a variable declaration is: \ on type identifier [= value Jf identifier [= value } ..]; Examples: : inta, b; ~a int a= 10, b= 20, ¢= 30; int a,b = 20,c¢; Confusion point In Java, variable name can also start with underscore character “_", or a dollar sign ‘$". Best Possible answer is chosen. A Deanery poeta} een Seca fed Download App MCQ Question 9 x View this Question Online > What is garbage collection in the context of Java? 1. The operating system periodically deletes all of the Java files available on the system, <3 2. When all references to an object are gone, then the memory used by the object is automatically teclaimed. 3. AM: package imported in a program and not being used, is automatically deleted. 4, The java virtual machine (JVM) checks the output of any java program and deleted anything that does not make sense at all. Answer (Detailed Solution Below) Option 2: When all references to an object are gone, then the memory used by the object is automatically reclaimed. Java MCQ Question 9 Detailed Solution Concept: - .. + Java garbage collection is the process of releasing unused memory. + Sometimes some objects are no longer required by the program and when there is no reference to an object, then that object should be released, + This process is known as garbage collection. Explanation: rg Ls + Garbage collection orc cone by JVM (java virtual machine). Unreachable and unusable objects are available fo collection. + With garbage collection, the programmer has no need to worry about dereferencing the object. It increases memory efficiency end less memory leakage. + For this, make the object reference null so that it is deleted by garbage collection. Example: obj obj1 = new obj(): obj = null, 1/ for garbage collection eee) Start Complete Exam Preparation Peay fered aes) DS tierce ccreesteoa ci pxotirarey Download App MCQ Question 10 ' Which of the following methods will create a string In Java? |: String S = ‘Hello Java"; I: String S2 = new String ("Hello Java"); 1. Only] ® 3. Both | andil 4. Neither! nor Il Answer (Detailed Solution Below) Option 3: Both | and Il Java MCQ Question 10 Detailed Solution The correct answer is option 3. View this Question Online > Concept: Astring is an object in Java that indicates a collection of characters or char Values. A Java string object is created using the java.lang String class. AString object can be created in one of two ways: \ By string literal: “ Java String literal is created by — Example: String s="Welcome”; By new keyword: Java String is created by using the keyword “new’. Example: String s=new String("Welcome’); Hence the correct answer is Both | and II. ee eo ac Start Complete Exam Preparation Pray Prac fi Nad acid Verte CRs ws Xetra Download App MCQ Question 11 View this Question Online > Acondition that is caused by run-time error in a computer program Is known as: 1. Syntax error 2. Fault 3. Semantic error “ Answer (Detailed Solution Below) Option 4: Exception Java MCQ Question 11 Detailed Solution Concept: In computer programming, an exception is a special condition encountered during program execution or run time. Example: if the program tries to open a file that does not exist, divide by zero, etc. ss Explanation: When an error occurs within a method, the method creates an object known as an exception object which contains information about the error, This process is known as throwing an exception. P After this, the run time system finds something to handle it. Run time system searches the list of methods that can handle the exception. Search begins with the method in which the error occurred and proceeds through the list of methods in reverse order. Exception handler chosen is said to catch the exception. ‘ There are two blocks to handle an exception: try end catch block. Try block is used to enclose the code that throws an exception. It is used within the method. Catch block is used to handle the exc y declaring the type of exception with the parameter. Catch block must be used after the ty b Syntax : try { ae AS am eLey ya) o) CM dd TLC) Cane ar crea ie aI (ice Download App MCQ Question 12 View this Question Online > The static keyword word is used in public static void main() declaration in Java. 5 1. To enable the JVM to make call to the main(), as class has not been instantiated. 2. To enable the JVM to make call to the main(), as class has not been inherited. 3. To enable the JVM to make call to the main(), as class has not been loaded. 4. To enable the JVM to make call to the main(), as class has not been finalized. Answer (Detailed Solution Below) Option 1 : To enable the JVM to make call to the main(), as class has not been instantiated. & eee ayo aren Start Complete Exam Preparation cay ee Pa (ss) Lea) MasterClasses ereerenras Excise Download App MCQ Question 13 View this Question Online In Java, the Dynamic Array are known as: 1, Vectors 2. Cycie 3. Remote 4. Kubernotos ‘Answer (Detailed Solution Below) Option 1 : Vectors eee) Start Complete Exam Preparation Peay fered oe ar DS tierce ccreesteoa ci pxotirarey Download App MCQ Question 14 Parent class of all java classes is 1. Java.lang.system 2. Java.lang.object 3, Java.lang.class 4. Java lang,reflect object Answer (Detailed Solution Below) Option 2: Java.lang.object View this Question Online > eee) Start Complete Exam Preparation Peay fered oe ar DS tierce ccreesteoa ci pxotirarey Download App MCQ Question 15 View this Question Online > Javaisa language. This means that you must be explicit about what type of data you are working with. 1. Strongly typed 2. Loosely typed 3. Dynamically typed 4. Weakly typed Answer (Detailed Solution Below) Option 1: Strongly typed

You might also like