Java Programming Unit-2 Quiz
37 Questions
0 Views

Java Programming Unit-2 Quiz

Created by
@BraveSerpentine819

Questions and Answers

Which of the following is an example of unboxing?

  • Converting a float to Float
  • Converting a Double to double (correct)
  • Converting an int to Integer
  • Converting a char to Character
  • Auto boxing refers to the automatic conversion of wrapper types into primitive types.

    False

    What is the wrapper class for the primitive type float?

    Float

    The automatic conversion of __________ into its corresponding wrapper class is known as auto boxing.

    <p>primitive data type</p> Signup and view all the answers

    Match the following primitive types with their corresponding wrapper classes:

    <p>boolean = Boolean int = Integer char = Character double = Double</p> Signup and view all the answers

    What type of variable must be initialized before it is used?

    <p>Local Variable</p> Signup and view all the answers

    Instance variables are accessible throughout the class and can be accessed using the class name directly.

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

    What keyword is used to declare a class variable in Java?

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

    Local variables are declared within a ______.

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

    Match the following variable types with their appropriate descriptions:

    <p>Local Variable = Declared within methods and has limited scope Instance Variable = Belongs to an instance of a class Class Variable = Shared among all instances of a class Static Variable = Declared with static keyword</p> Signup and view all the answers

    What will happen if a local variable is used without being initialized?

    <p>A compile-time error will occur</p> Signup and view all the answers

    Each instance of a class in Java has its own copy of class variables.

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

    Name a characteristic of instance variables.

    <p>Each instance of a class has its own copy</p> Signup and view all the answers

    What is a class variable in Java?

    <p>A variable shared among all instances of a class.</p> Signup and view all the answers

    Implicit casting occurs when assigning a value of a larger data type to a smaller data type.

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

    What is the main characteristic of explicit casting?

    <p>It requires manual conversion by the programmer.</p> Signup and view all the answers

    A class variable can be accessed using the ______ name itself.

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

    Match the following types of casting with their description:

    <p>Implicit Casting = Automatic conversion from smaller to larger data type Explicit Casting = Manual conversion from larger to smaller data type</p> Signup and view all the answers

    In the example provided, what is the initial value of the class variable 'count'?

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

    In Java, narrowing casting is performed automatically.

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

    What happens during implicit casting?

    <p>Automatic conversion of compatible data types.</p> Signup and view all the answers

    What does the else statement do in an if-else structure?

    <p>Executes when the condition is false</p> Signup and view all the answers

    An if-else-if ladder can only contain two conditions.

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

    What is the purpose of a nested if-statement?

    <p>To include an if or if-else statement within another if or else-if statement.</p> Signup and view all the answers

    In the given example, the variables x and y are initialized with the values of _____ and _____ respectively.

    <p>10, 12</p> Signup and view all the answers

    Match the following programming constructs with their descriptions:

    <p>if statement = Executes a block of code based on a condition else statement = Executes a block of code when the condition is false if-else-if ladder = Allows multiple conditions to be checked in sequence nested if-statement = An if-statement inside another if-statement</p> Signup and view all the answers

    What will the output be if the variable city is initialized to 'Noida' in the provided else-if example?

    <p>city is noida</p> Signup and view all the answers

    In Java, you can use the == operator to compare strings.

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

    What is the main difference between an if statement and an if-else statement?

    <p>An if statement only executes a block of code if the condition is true, while an if-else statement provides an alternative block of code to execute when the condition is false.</p> Signup and view all the answers

    What will be printed if the address is 'Delhi, India' and the address does not contain 'Meerut' or 'Noida'?

    <p>[Ljava.lang.String;@hashcode</p> Signup and view all the answers

    A switch statement in Java can have duplicate case values.

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

    What keyword is used to terminate a switch statement in Java?

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

    The structure used to choose between multiple options based on the value of an expression in Java is called a ______.

    <p>switch statement</p> Signup and view all the answers

    Match the following programming concepts with their descriptions:

    <p>if statement = Checks a condition and executes a block of code if true else statement = Executes a block of code if the associated if condition is false switch statement = Selects one of many code blocks to execute based on an expression break statement = Terminates the current switch block or loop</p> Signup and view all the answers

    Which of the following is true regarding the switch statement in Java?

    <p>It supports case variables as String since version 7.</p> Signup and view all the answers

    In a switch statement, if no break statement is present, execution will continue to the next case.

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

    What will be the output if an address ends with 'Canada' in the provided example?

    <p>You are not living in India</p> Signup and view all the answers

    Study Notes

    Variable and Its Type

    • Variables are named storage locations that hold values of specific data types, enabling data manipulation within a program.
    • Local Variables:
      • Declared within methods, constructors, or code blocks.
      • Accessible only within the scope they are declared and must be initialized before use.
      • Lifespan is limited to the block of code.
    • Instance Variables (Non-Static Variables):
      • Declared within a class but outside methods, constructors, or blocks.
      • Each class instance has its own copy; initialized with default values if not explicitly assigned.
      • Accessible throughout the class using the object of that class.
    • Class Variables (Static Variables):
      • Declared with the static keyword; shared among all instances of the class.
      • Only one copy exists across all instances, initialized with default values if not given.

    Type Casting and Conversion

    • Type casting is the conversion of a value from one data type to another in Java.
    • Implicit Casting (Widening):
      • Automatic conversion between compatible data types, typically from a smaller to larger type.
      • Example: Converting an integer to a double (int to double).
    • Explicit Casting (Narrowing):
      • Manual conversion from a higher data type to a lower data type, not done automatically by Java.
      • Requires explicit instructions from the programmer.

    Auto Boxing and Unboxing

    • Auto Boxing:
      • Automatic conversion of primitive data types to their corresponding wrapper classes (e.g., int to Integer).
      • Simplifies assigning primitive values to wrapper class objects.
    • Unboxing:
      • Automatic conversion of wrapper types back to their corresponding primitive types, the reverse of auto boxing.

    Control Flow Statements

    • If Statement:
      • Executes specific code blocks based on boolean conditions with optional else clauses.
      • Syntax allows chaining conditions with else-if branches.
    • Nested If Statement:
      • An if statement can contain another if or if-else statement, allowing for complex decision-making.
      • Enables checking multiple conditions layered within each other.

    Switch Statement

    • Used for decision-making where multiple possible execution paths are based on the value of a variable.
    • Facilitates readability and organization compared to multiple if-else statements.
    • Notes on switch statements:
      • Cases can be int, short, byte, char, string, or enum types.
      • No duplicate cases allowed.
      • The default statement executes if no cases match the expression; it's optional.
      • A break statement exits the switch block after executing a case, preventing fall-through to the next case.

    This summary encapsulates the fundamental concepts of control flow and arrays in Java, focusing on variable types, casting, and control structures, essential for enhancing programming skills.

    Studying That Suits You

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

    Quiz Team

    Description

    Test your knowledge on Control Flow and Arrays in Java in this Unit-2 quiz. Explore the different types of variables, their usage, and the concepts that underpin Java programming. Perfect for students looking to solidify their understanding of these foundational topics.

    Use Quizgecko on...
    Browser
    Browser