🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Java Programming Concepts
61 Questions
0 Views

Java Programming Concepts

Created by
@FairMookaite1351

Podcast Beta

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will be the output of the program? public class Test { public static void main(String[] args) { int[] arr1 = {11,12,13,14,14,15}; int[] arr2 = arr1; arr1 = new int; for(int i = 0; i < arr1.length; i++) System.out.print(arr2[i] + ' '); } }

  • 11 12 13 (correct)
  • 11 12 13 14 15
  • 0 0 0
  • 0 0 0 0 0
  • How many String objects will be created when this method is invoked? public String makingStrings() { String str = 'Welcome'; str = str.substring(1, 5); str = str.toUpperCase(); return str; }

  • 2
  • 4
  • 1
  • 3 (correct)
  • Predict the output: public abstract class Abs { public Abs(){ System.out.println('Constructor from Abstract class'); } } public class Test extends Abs { public static void main(String args[]){ Abs obj=new Test(); } }

  • Constructor from Abstract class (correct)
  • Compile time error: An abstract class cannot be instantiated
  • Program will execute successfully but not display anything
  • Compile time error: An abstract class cannot have a constructor
  • What is the result of the following code snippet? public static void test(String str) { if (str == null || str.length() == 0) { System.out.println('String is empty'); } else { System.out.println('String is not empty'); } } public static void main(String a[]) { test(null); }

    <p>An exception is thrown at runtime.</p> Signup and view all the answers

    To copy elements from one array to another array, Java provides efficient built-in methods in ________ class.

    <p>Arrays</p> Signup and view all the answers

    In Java, if you try to compile the below code: class MyStringDemo extends String { }

    <p>False</p> Signup and view all the answers

    What will be the output of the code snippet? public class StringDemo { public static void main(String[]args) { String s1 = new String('java'); String s2 = new String('java'); System.out.println(s2.compareTo(s1)); } }

    <p>0</p> Signup and view all the answers

    What will be the output of the program? public class Test { public static void main (String args[]) { String str = NULL; System.out.println(str); } }

    <p>Compile time error</p> Signup and view all the answers

    What can be the valid access specifier for the calculate method in Calculator class so that it can be accessed from CalculatorService class?

    <p>public</p> Signup and view all the answers

    What is required at line 4 in class Library to use the countBook method of Book class?

    <p>Library class cannot use the countBook method in Book class.</p> Signup and view all the answers

    Rearrange this code correctly.

    <p>package test; import java.util.Scanner; public class Main { //Some code here }</p> Signup and view all the answers

    In main to create an object of Book, which of the following are valid?

    <p>p1.Book bookObj=new p1.Book();</p> Signup and view all the answers

    Which two pairs of method declarations follow the JavaBeans standard for accessing the field 'enabled'?

    <p>public void setEnabled(boolean enabled) / public boolean isEnabled()</p> Signup and view all the answers

    Predict the Output of the following Java Program.

    <p>Compile Time Error</p> Signup and view all the answers

    Identify the true statement(s) about constructors.

    <p>1 and 2</p> Signup and view all the answers

    Identify which statement is true about constructors.

    <p>Constructor can be overloaded</p> Signup and view all the answers

    What is the result of the given Java program?

    <p>Compile Time Error</p> Signup and view all the answers

    Predict the output: public class Trial { public static void main(String[] args) { int arr={ }; System.out.print(arr); } }

    <p>Compile time error</p> Signup and view all the answers

    What will be the output of the above code snippet? int arr[]={12,4,22,5,1,66}; int position = Arrays.binarySearch(arr,5); System.out.println(position);

    <p>Unpredictable</p> Signup and view all the answers

    Abstract methods cannot be final.

    <p>True</p> Signup and view all the answers

    An abstract class can have non-abstract methods also.

    <p>true</p> Signup and view all the answers

    Predict the output of the following program: abstract class Demo { public int a; Demo() { a = 10; } abstract public void set(); abstract final public void get(); } class Test extends Demo { public void set(int a) { this.a = a; } final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.set(20); obj.get(); }

    <p>Compilation error</p> Signup and view all the answers

    Will the code below execute successfully?

    <p>False</p> Signup and view all the answers

    What is the output of following Java Program? abstract class Demo { public int a; Demo() { a = 10; } abstract public void set(); } class Test extends Demo { final public void get() { System.out.println("a = " + a); } public static void main(String[] args) { Test obj = new Test(); obj.get(); }

    <p>Compile Time Error</p> Signup and view all the answers

    Will the below code execute successfully?

    <p>False</p> Signup and view all the answers

    Which method at line 14 will correctly complete class Car?

    <p>[protected] void display() { }</p> Signup and view all the answers

    Given a method in a public class, a private access modifier must be used to restrict access to that method to only other members of the same class.

    <p>True</p> Signup and view all the answers

    What will be the output of the following program?

    <p>Class FourWheeler, Class Car, Class Audi</p> Signup and view all the answers

    Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships between these classes?

    <p>Computer is the superclass, AppleComputer and IBMComputer are subclasses of computer</p> Signup and view all the answers

    What is the result when the given code is executed?

    <p>123</p> Signup and view all the answers

    Which members of a class can be accessed by other classes is determined by the ________________

    <p>Access specifier</p> Signup and view all the answers

    What does this() mean in constructor chaining concept?

    <p>Used for calling the no argument constructor of the same class.</p> Signup and view all the answers

    Match the following programming languages with their primary usage:

    <p>Book b = new Magazine(); = Upcasting Magazine m = (Magazine) b = Downcasting</p> Signup and view all the answers

    Which of the following statements regarding superclass and subclass types is true?

    <p>Object of subclass referenced by superclass type can invoke superclass methods</p> Signup and view all the answers

    Default Access is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package.

    <p>True</p> Signup and view all the answers

    In Java, what happens when a child class is declared in a different package and needs to access a parent class variable?

    <p>The variable in the parent class must be declared protected</p> Signup and view all the answers

    Child class objects can be instantiated when the parent class constructor is protected.

    <p>True</p> Signup and view all the answers

    What will be the output of the following Java code snippet?

    <p>Compile time error</p> Signup and view all the answers

    Predict the output. What will be the output of the following program?

    <p>The file will compile without error.</p> Signup and view all the answers

    The type Vehicle has drive functionality. The classes Car and Bike implements the drive functionality and can be further subclassed. Fill in the given code with appropriate access specifier so that the subclasses of Car and Bike do not modify the Drive functionality.

    <p>public final</p> Signup and view all the answers

    Predict the output. What is the output of the given code?

    <p>Number</p> Signup and view all the answers

    Predict the output: Interface Employee. Class PermanentEmployee implements Employee. What error will occur when executing the provided code?

    <p>Error, since variable a is assigned a value</p> Signup and view all the answers

    An instance of an interface can be created.

    <p>False</p> Signup and view all the answers

    What is the output of the above code?

    <p>Get FourWheelers Cars displayed FourWheelers displayed</p> Signup and view all the answers

    If you have 'final class Test {}', how to create a reference for the class Test while inheriting it?

    <p>Compilation Error: we can't inherit the class which is 'final'</p> Signup and view all the answers

    What will be the output of the program?

    <p>Pine</p> Signup and view all the answers

    The equals() method takes the reference of Object as a parameter.

    <p>True</p> Signup and view all the answers

    Final methods can/can't be overridden.

    <p>Can't be overridden</p> Signup and view all the answers

    Which three statements are true?

    <p>A method with the same signature as a private final method in class X can be implemented in a subclass of X.</p> Signup and view all the answers

    What is the output of the 'Calculator' class code snippet?

    <p>5 is the only operand supplied Two operands supplied are multiplied</p> Signup and view all the answers

    Which members of a class can be accessed by other classes is determined by the ________________

    <p>Access specifier</p> Signup and view all the answers

    What does this() mean in constructor chaining concept?

    <p>Used for calling the no argument constructor of the same class.</p> Signup and view all the answers

    Match the following programming concepts:

    <p>Book b = new Magazine(); = Upcasting Magazine m = (Magazine) b = Downcasting</p> Signup and view all the answers

    Predict the output. What will be the output of the following program?

    <p>The file will compile without error.</p> Signup and view all the answers

    The type Vehicle has drive functionality. The classes Car and Bike implements the drive functionality and can be further subclassed. Fill in the given code with appropriate access specifier so that the subclasses of Car and Bike do not modify the Drive functionality.

    <p>final</p> Signup and view all the answers

    What will be the output of the given code snippet?

    <p>error, since variable a is assigned a value</p> Signup and view all the answers

    An instance of an interface can be created.

    <p>False</p> Signup and view all the answers

    A class can implement multiple interfaces.

    <p>True</p> Signup and view all the answers

    What is the correct access specifier for the given class definition: public ______ class Car implements Insurance { }

    <p>abstract</p> Signup and view all the answers

    If a class implements two interfaces with default methods of the same name and signature but different implementations, a conflict will arise due to ambiguity.

    <p>True</p> Signup and view all the answers

    What will be the output of the following program?

    <p>Runtime Error</p> Signup and view all the answers

    Study Notes

    Packages and Access Specifiers

    • A method in one package can be accessed from another package if it is declared public.
    • If a method is declared private, it cannot be accessed from outside the class.
    • The correct access specifier for the calculate method in the Calculator class is public.

    Importing Classes

    • To use a class from another package, it needs to be imported.
    • The correct statement to import the static variable PI is import static java.lang.Math.PI;.

    JavaBeans Standard

    • When writing getters and setters, setters return type is void and getters return type is the corresponding data type.
    • The naming convention for getters and setters is camelcase notation.
    • For boolean return type, it should start with 'is' or 'are' followed by the field name.

    Constructors

    • When no constructor is written in a class, the compiler creates a default constructor.
    • The default constructor will implicitly invoke the default/no-argument constructor of the superclass.
    • If a class has a parameterized constructor alone, then the compiler will not create a default constructor.
    • Constructors can be overloaded.

    Java Program Output

    • In the given Java program, there is a compile-time error because the instance variable x is accessed from a static context.

    Constructors Chaining

    • this() is used to call the no-argument constructor of the same class.

    Inheritance and Polymorphism

    • Upcasting is when a subclass object is assigned to a superclass reference.
    • Downcasting is when a superclass reference is cast to a subclass object.

    Abstract Classes and Interfaces

    • An abstract class can have both abstract and non-abstract methods.
    • A default method in an interface can only be public.
    • An abstract class can be declared as final if it should not be subclassed.
    • If a class inherits an abstract class, it should provide an implementation for all the abstract methods in the parent class.
    • The super keyword is used to invoke the overridden method in the superclass.### Inheritance and Polymorphism
    • Code reusability and runtime polymorphism can be achieved through inheritance.
    • A method in an interface, when implemented, should be either static or default.

    Lambda Expressions

    • A correct lambda expression for the Functional method int findMax(int a, int b) is (int a, int b) -&gt; { int min = a &gt; b ? a : b; return min; }.

    Abstract Classes

    • An abstract class can have non-abstract methods.
    • An abstract class cannot be instantiated.
    • In the constructor of a child class, the first line should be a call to the superclass constructor. If not written, then implicitly it invokes the superclass constructor as super().

    Constructors and Inheritance

    • When a child class is instantiated, the constructor of the abstract class is called.
    • The output of the code class A { String name = "A"; public String getName() { return name; } String greeting() { return "class A"; } } class B extends A { String name = "B"; String greeting() { return "class B"; } } public class Test { public static void main(String arg[]) { A a = new A(); A b = new B(); System.out.println(a.greeting() + " has name " + a.getName()); System.out.println(b.greeting() + " has name " + b.getName()); } } is class A has name A and class B has name A.

    String Methods

    • The toUpperCase() method converts a string to uppercase.
    • The substring() method returns a new string that is a subset of the original string.
    • The equals() method checks if two strings are equal.
    • The Arrays.equals() method checks if two arrays are equal.

    Arrays and String

    • When an array is assigned to another array, it does not create a new copy of the array. Instead, it references the same array.
    • The Arrays.equals() method can be used to compare two arrays.

    Compilation and Runtime Errors

    • A compile-time error occurs when the code does not compile due to syntax errors.
    • A runtime error occurs when the code compiles but throws an exception at runtime.
    • The Arrays.binarySearch() method can be used to search for an element in a sorted array.
    • If the array is not sorted, the Arrays.binarySearch() method will not work correctly.

    Method Overloading and Overriding

    • Method overriding is when a subclass provides a different implementation of a method that is already defined in its superclass.
    • Method overloading is when multiple methods with the same name can be defined, but with different parameter lists.### Inheritance in Java
    • Abstract methods cannot be final.
    • Protected methods in a superclass cannot be made private in a subclass.
    • An abstract class can have both abstract and non-abstract methods.
    • Abstract methods must be implemented by a subclass.
    • A subclass can extend only one abstract class.
    • A subclass can implement multiple interfaces.

    Method Overriding

    • When a subclass provides a specific implementation for a method that is already defined in its superclass, it is called method overriding.
    • The method in the subclass must have the same name, return type, and parameter list as the method in the superclass.

    Method Hiding

    • When a subclass provides a method with the same name as a method in its superclass, but with a different parameter list, it is called method hiding.
    • The method in the subclass hides the method in the superclass, but it can still be accessed by using the super keyword.

    Constructors and Inheritance

    • A subclass constructor must call the superclass constructor using the super keyword.
    • If a subclass does not explicitly call the superclass constructor, the compiler will automatically insert a call to the no-argument constructor of the superclass.

    Access Modifiers and Inheritance

    • A protected member of a superclass can be accessed by its subclass, even if it is in a different package.
    • A default member of a superclass in one package cannot be accessed by its subclass in a different package, unless it is declared as protected.

    Inheritance and Constructors

    • When a subclass is created, its constructor is called, which in turn calls the constructor of its superclass.
    • The order of constructor calls is from the top-level class to the bottom-level class.

    Multiple Inheritance

    • Java does not support multiple inheritance, where a subclass can extend multiple classes.
    • However, a subclass can implement multiple interfaces.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Java.pdf

    Description

    This quiz covers advanced Java concepts including abstract classes, string manipulation, and array operations.

    More Quizzes Like This

    Java String (Basic)
    30 questions

    Java String (Basic)

    AwedExuberance avatar
    AwedExuberance
    Java String (Hard)
    30 questions

    Java String (Hard)

    AwedExuberance avatar
    AwedExuberance
    Java String Class Methods
    12 questions
    Use Quizgecko on...
    Browser
    Browser