Object-Oriented Programming Concepts

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What represents an entity in the real world that can be distinctly identified?

An object

What is a construct that defines objects of the same type?

A class

An object is an instance of a __________.

class

The keyword __________ is required to declare a class.

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

What is invoked to create an object?

<p>A constructor</p> Signup and view all the answers

Which of the following statements are true? (Select all that apply)

<p>A default constructor is a no-arg constructor. (A), A default constructor is provided automatically if no constructors are explicitly declared in the class. (B)</p> Signup and view all the answers

Which of the following statements are true? (Select all that apply)

<p>Constructors are invoked using the new operator when an object is created. (A), Constructors do not have a return type, not even void. (B), Multiple constructors can be defined in a class. (C), Constructors must have the same name as the class itself. (D)</p> Signup and view all the answers

Analyze the following code and choose the correct statements:

public class Test {
 public static void main(String[] args) {
 A a = new A();
 a.print();
 }
}

class A {
 String s;

 A(String s) {
 this.s = s;}

 void print() {
 System.out.println(s);
 }
}

<p>The program would compile and run if you change A a = new A() to A a = new A(&quot;5&quot;). (A), The program has a compile error because class A does not have a default constructor. (B)</p> Signup and view all the answers

Analyze the following code and choose the correct statement:

class TempClass {
 int i;
 public void TempClass(int j) {
 int i = j;
 }
}

public class C {
 public static void main(String[] args) {
 TempClass temp = new TempClass(2);
 }
}

<p>The program has a compile error because TempClass does not have a constructor with an int argument. (A)</p> Signup and view all the answers

Given the declaration Circle x = new Circle(), which of the following statements is most accurate?

<p>x contains a reference to a Circle object.</p> Signup and view all the answers

What happens when Test test = null; is executed followed by System.out.println(test.x);?

<p>The program has a runtime NullPointerException because test is null while executing test.x.</p> Signup and view all the answers

The default value for data field of a boolean type, numeric type, and object type is __________, respectively.

<p>false, 0, null</p> Signup and view all the answers

Which of the following statements are true? (Select all that apply)

<p>Local variables do not have default values. (A), A variable of a reference type holds a reference to where an object is stored in the memory. (B), A variable of a primitive type holds a value of the primitive type. (C), Data fields have default values. (D)</p> Signup and view all the answers

Analyze the following code and find the reason for compile errors:

public class Test {
 public static void main(String[] args) {
 double radius;
 final double PI= 3.15169;
 double area = radius * radius * PI;
 System.out.println("Area is " + area);
 }
}

<p>The program has compile errors because the variable radius is not initialized. (A)</p> Signup and view all the answers

Analyze the following code and identify the issue:

public class Test {
 int x;

 public Test(String t) {
 System.out.println("Test");}

 public static void main(String[] args) {
 Test test = new Test();
 System.out.println(test.x);
 }
}

<p>The program has a compile error because Test does not have a default constructor. (A)</p> Signup and view all the answers

What is the outcome of compiling TestCircle.java and then Circle.java?

<p>Both compile fine.</p> Signup and view all the answers

Which of the following statements are correct? (Select all that apply)

<p>A data field in a class can be of an object type. (A), A reference variable references to an object. (B)</p> Signup and view all the answers

Which of the following code A or B, or both create an object of the Date class? (Select all that apply)

<p>public class Test { public Test() { new java.util.Date(); } } (A), public class Test { public Test() { java.util.Date date = new java.util.Date(); } } (B)</p> Signup and view all the answers

What can be placed in the blank line in the code?

public class Test {
 private int id;
 public void m1() {
 _____.id = 45;
 }
}```

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

Analyze the following code and outline its behavior:

class Test {
 private double i;
 public Test(double i) {
 this.t();
 this.i = i;
 }
 public Test() {
 System.out.println("Default constructor");
 this(1);
 }
 public void t() {
 System.out.println("Invoking t");
 }
}```

<p>this.t() may be replaced by t(). (A), this(1) must be called before System.out.println(&quot;Default constructor&quot;). (B)</p> Signup and view all the answers

Analyze the following code and determine its behavior:

class Circle {
 private double radius;
 public Circle(double radius) {
 radius = radius;
 }
}```

<p>The program will compile, but you cannot create an object of Circle with a specified radius. (A), The object will always have radius 0. (B)</p> Signup and view all the answers

You can declare two variables with the same name in __________.

<p>different methods in a class</p> Signup and view all the answers

What is the output for the third statement in the main method from the following Java code?

public class Foo {
 static int i = 0;
 static int j = 0;
 public static void main(String[] args) {
 int i = 2;
 int k = 3;
 {
 int j = 3;
 System.out.println("i + j is " + i + j);
 }
 k = i + j;
 System.out.println("k is " + k);
 System.out.println("j is " + j);
 }
}```

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

What is the output for the second statement in the main method from the following Java code?

public class Foo {
 static int i = 0;
 static int j = 0;
 public static void main(String[] args) {
 int i = 2;
 int k = 3;
 {
 int j = 3;
 System.out.println("i + j is " + i + j);
 }
 k = i + j;
 System.out.println("k is " + k);
 System.out.println("j is " + j);
 }
}```

<p>k is 2</p> Signup and view all the answers

What is the output for the first statement in the main method from the following Java code?

public class Foo {
 static int i = 0;
 static int j = 0;
 public static void main(String[] args) {
 int i = 2;
 int k = 3;
 {
 int j = 3;
 System.out.println("i + j is " + i + j);
 }
 k = i + j;
 System.out.println("k is " + k);
 System.out.println("j is " + j);
 }
}```

<p>i + j is 23</p> Signup and view all the answers

Which of the following statements are true about an immutable object? (Select all that apply)

<p>All properties of an immutable object must be private. (A), The contents of an immutable object cannot be modified. (B), An immutable object contains no mutator methods. (C), A readable object type property in an immutable object must also be immutable. (D)</p> Signup and view all the answers

Assume java.util.Date[] dates = new java.util.Date, which of the following statements are true? (Select all that apply)

<p>dates = new java.util.Date is fine, which assigns a new array to dates. (A), dates is null. (B)</p> Signup and view all the answers

Given the declaration Circle[] x = new Circle, which of the following statements is most accurate?

<p>x contains a reference to an array and each element in the array can hold a reference to a Circle object.</p> Signup and view all the answers

What is the output of the following program:

import java.util.Date;
public class Test {
 public static void main(String[] args) {
 Date date = new Date(1234567);
 m1(date);
 System.out.print(date.getTime() + " ");
 m2(date);
 System.out.println(date.getTime());
 }
 public static void m1(Date date) {
 date = new Date(7654321);
 }
 public static void m2(Date date) {
 date.setTime(7654321);
 }
}```

<p>1234567 7654321</p> Signup and view all the answers

What is the value of times displayed?

public class Test {public static void main(String[] args) {
 Count myCount = new Count();
 int times = 0;
 for (int i=0; i

Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Object-Oriented Concepts

  • An object represents a real-world entity that can be distinctly identified.
  • A class is a construct that defines objects of the same type; it serves as a blueprint for creating objects.
  • An object is an instance of a class and is created using a constructor.
  • The keyword class is required to declare a class in Java.

Constructors

  • A constructor is invoked to create an object.
  • If no constructors are explicitly declared in a class, a default constructor is provided automatically.
  • Constructors do not have a return type and must share the same name as the class.
  • Multiple constructors can exist within a class.
  • Constructors are invoked using the new operator.

Code Analysis and Errors

  • Programs can fail to compile if a default constructor is absent when one is invoked.
  • Local variables do not receive default values; however, fields (data members) do have default values (e.g., boolean is false, numeric is 0, object type is null).
  • A program will compile if local variables are properly initialized, otherwise, it will result in compile-time errors.

Variable Scope and Behavior

  • You can declare variables with the same name in different methods within a class.
  • Variables within a block can overshadow class-level variables, leading to varying values based on scope.
  • Static variables retain their values across class instances and are shared among all instances.

Immutable Objects

  • An immutable object's properties cannot be modified once set.
  • All properties of immutable objects should be private, and they should not have mutator methods.
  • A readable property in an immutable object must also be immutable itself.

Arrays and References

  • When declaring an array of objects, such as Circle[] x, it indicates that x references an array where each element can point to a Circle object.
  • Assigning an array or creating an array must be done correctly to avoid null references.

Method Behaviors

  • When passing objects to methods, if the reference is reassigned, it does not affect the original object unless the object's properties are modified directly.
  • The newly assigned object inside a method does not alter the original object's reference.

Output Analysis

  • Understanding specific outputs from programming statements requires examining variable scopes, initializations, and method invocations.
  • Outputs reflect the status of static and instance variables as influenced by method executions and scope resolutions.

Code Compliance and Common Pitfalls

  • Incorrect constructor invocation leads to compile errors.
  • Failing to initialize variables before use will cause compile-time or runtime exceptions.
  • Ensure clarity in variable scope to avoid confusion surrounding local vs. class-level variables.

Studying That Suits You

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

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser