Comp sci test

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What does the expression "x += 5" do in Java?

adds 5 to x

!

not

||

or

public static void main(String[] args) { Subclass obj = new Subclass(); obj.display(); } } class Superclass { void display() { System.out.println("Superclass"); } } class Subclass extends Superclass { void display() { System.out.println("Subclass"); super.display(); } }

output is "Subclass" followed by "Superclass".

Which of the following is used for reading user input in Java?

scanner

What is the logical AND operator in Java?

&&

What is the output of the following code snippet? ```java int i = 0; while (i < 3) { System.out.print(i + " "); i++; }

0 1 2

public class AccessModifiersExample { public static void main(String[] args) { MyClass obj = new MyClass(); obj.printValue(); } } class MyClass { private int value = 5; void printValue() { System.out.println(value); } } ```

5

public class Example { public static void main(String[] args) { int x = 5; System.out.println(x++); System.out.println(++x);

5 7

public class GuessOutput7 { public static void main(String[] args) { int x = 5; do { System.out.print(x + " "); x--; } while (x > 0); } }

5 4 3 2 1

What is a class in Java?

A class in Java is the user defined blue print with fields and methods.

In Java, what is an object?

An instance of a class

How can you check if a number is a perfect square in Java?

By comparing the number with its square root

What is the result of the following code snippet? ```java int x = 5; int y = 10; if (x > y) { System.out.println("x is greater"); } else { System.out.println("y is greater"); }

Y is greater

public class GuessOutput6 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } } }

* **

Nested while/do-while Loops

- Similar to nested for loops but using while or do-while statements.

*while Loop

- Syntax: `while (condition) { // code to repeat }` - Repeats a block of code as long as the specified condition is true.

Super Keyword:**

- Used to call methods or access fields of the superclass from within the subclass. - `super.method()` or `super.field`.

Nested for Loops

- Using one or more for loops inside another. - Useful for iterating over 2D arrays or performing operations on multiple levels.

What is the default value of the `int` data type in Java?

0

for (int j = 0; j < 4; j++) { if (j == 2) { break; } System.out.print(j + " "); }

0 1

int i = 0; while (i < 3) { System.out.print(i + " "); i++; }

0 1 2

What will the following code output? ```java for (int i = 0; i < 5; i++) { System.out.print(i + " "); }

0 1 2 3 4

```java public class GuessOutput9 { public static void main(String[] args) { int n = 4; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } } }

1 12 123 1234

public class GuessOutput3 { public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { System.out.print(i * j + " "); } } } }

1 2 2 4 3 6

public class ConstructorExample { public static void main(String[] args) { MyClass obj = new MyClass(); System.out.println(obj.getValue()); } } class MyClass { private int value; MyClass() { value = 10; } int getValue() { return value; } }

10

public class GuessOutput4 { public static void main(String[] args) { int x = 10; while (x > 0) { System.out.println(x); x -= 3; } } }

10 7 4 1

int a = 5; int b = 2; System.out.println(a / b);

2

What is the result of 15 % 4?

3

What is the result of the expression `10 / 3` in Java?

3

What is the output of `System.out.println("5" + 2)` in Java?

52

Which operator is used to compare two values for equality in Java?

==

What is the difference between `==` and `.equals()` in Java when comparing strings?

==` compares references, `.equals()` compares values

What is the purpose of the `static` keyword in Java?

?

Class Definition

A class is a blueprint for creating objects. - It defines the attributes (fields) and behaviors (methods) common to all objects of that type.

What is a constructor in Java?

A special method for object initialization

Constructor

A special method used for initializing objects. - It has the same name as the class and is invoked when an object is created.

What is a variable?

A variable is a named storage location in a program, It holds data that can be changed during the execution of the program

How can you represent an infinite loop in Java? a) `for ( ; ; ) { }` b) `while (true) { }` c) `do { } while (true)` d) All of the above

All of the above

Inheritance

Allows a class (subclass/derived class) to inherit properties and behaviors of another class (superclass/base class). - `extends` keyword is used for inheritance.

How is the 'switch' statement different from 'if-else' statements in Java?

An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.

Declaration and Initialization

Declare a variable using the syntax: `dataType variableName;` - Initialize a variable at the time of declaration or later using: `variableName = value;`

Encapsulation

Encapsulation is the bundling of data and methods that operate on that data within a single unit, i.e., a class. - Access modifiers (`public`, `private`, `protected`) control access to class members.

public class GuessOutput1 { public static void main(String[] args) { int x = 5; if (x > 0) { System.out.println("Hello"); } else { System.out.println("World"); } } }

Hello

What is polymorphism in Java?

It is when a single variable is used with several different types of related objects at different places in a program.

Which keyword is used to create an object in Java?

New

public class GuessOutput5 { public static void main(String[] args) { int num = 7; if (num % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } } }

Odd

What is the purpose of the modulus operator (%) in Java?

Returns the remainder of division

Overriding Methods

Subclasses can provide a specific implementation for a method already defined in the superclass. - Use `@Override` annotation to indicate that a method is intended to override a superclass method.

What is encapsulation?

Surrounding something, not just to keep the contents together, but also to protect those contents. Restricts access to the inner workings of a class or any objects based on that class; this is referred to as information hiding or data hiding.

What is the value of the expression (5 > 3) && (7 < 10)?

True

Naming Rules

Variable names are case-sensitive. - Must begin with a letter, underscore, or dollar sign. - Subsequent characters may also be numbers.

do-while Loop

```java do { // code to repeat } while (condition); ``` - Similar to a while loop, but guarantees the code block executes at least once.

What is the result of the expression 10 / 3 in Java?

b

public class PolymorphismExample { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.makeSound(); cat.makeSound(); } } class Animal { void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } class Cat extends Animal { void makeSound() { System.out.println("Meow"); } }

bark meow

Which keyword is used to exit a loop prematurely in Java?

break

Which of the following is used to exit a loop prematurely in Java?

break

Primitive Data Types

byte, short, int, long, float, double, char, boolean

Which of the following is an example of a primitive data type in Java?

char

Which keyword is used to continue to the next iteration of a loop in Java?

continue

Which loop is used when you want to execute a block of code a and then check a condition in Java?

do-while

In Java, which loop is suitable for situations where the loop body must execute at least once?

do-while loop

Which of the following is a primitive data type in Java?

double

Which keyword is used for inheritance in Java?

extends

What is the result of the expression (true || false) && (true && false)?

false

*Which keyword is used to declare a constant in Java?

final

In Java, which keyword is used to declare a constant variable?

final

Which of the following is not a valid modifier in Java?

final, static, abstract

How is an object created in Java?

new

Which access modifier restricts access the least in Java?

public

What is the purpose of the 'this' keyword in Java?

remove ambiguity between variables within the class and parameter names passed to methods and constructors Reference the object itself from within a class

public class GuessOutput10 { public static void main(String[] args) { int x = 7; switch (x) { case 5: System.out.println("Five"); break; case 7: System.out.println("Seven"); case 10: System.out.println("Ten"); default: System.out.println("Default"); } }

seven ten default

public class GuessOutput8 { public static void main(String[] args) { int a = 2, b = 3; if (a == 2 || b == 3) { System.out.println("True"); } else { System.out.println("False"); } }

true

Which keyword is used to define a method that does not return any value in Java?

void

public class GuessOutput2 { public static void main(String[] args) { int i = 0; while (i < 5) { System.out.print(i + " "); i++; } } }

0 1 2 3 4

&&

And

What does the `super` keyword do in Java?

Calls a method from the superclass

Which of the following is NOT a primitive data type in Java?

Class

What is the purpose of the conditional operator (ternary operator) in Java?

Compact representation of if-else statements

What does the '+' operator do for strings?

Concatenation of two strings

What does the relational operator "!=" represent in Java?

Not equal to

Polymorphism

Objects of a subclass can be treated as objects of the superclass. - Achieved through method overloading and overriding.

What does the `@Override` annotation indicate?

Overriding a method

for Loop

Syntax: `for (initialization; condition; update) { // code to repeat }` - Executes a block of code repeatedly as long as the condition is true.

What is the purpose of the "else if" statement in Java?

To check multiple conditions sequentially

What is the purpose of the "if" statement in Java?

To execute a block of code only if a certain condition is true

What is the purpose of the `break` statement in Java?

To exit a loop or switch statement

What is the purpose of the `super` keyword in Java?

To invoke the superclass method

What is the purpose of the 'for' loop in Java?

To iterate over a sequence of statements

What is the purpose of nested loops in Java?

To iterate over a sequence.

*What is the main purpose of the `this` keyword in Java?

To refer to the current instance of the class

What is the purpose of the 'else' clause in an 'if-else' statement in Java?

Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.

In Java, what is the role of the 'if' statement?

Use the if statement to specify a block of Java code to be executed if a condition is true


संबंधित स्टडी सेट्स

Prep U Musculoskeletal Chapter 53

View Set

Grammar and Context Free Grammars

View Set

Salesforce.com ADM 201 Study Guide - Practice 1

View Set

A&P Chapter 19 Heart & Cardiovascular Function

View Set

4B - SHERPATH - CHAP. 43 - care of the pt with a integumentary disorder - 10/7/22

View Set

Honors Science Worksheet 5-56 Structure of a Typical Flower

View Set

BJU Economics Chapter 6 - Economic Systems (control civilizations)

View Set

Radha Appan ISQS 4348 Exam 1 review

View Set