answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

Given an int array length 2 return true if it contains a 2 or a 3.?

public boolean has2or3(int[] nums) {

if ( nums[0] 3 )

return true;

return false;

}

How do you create an object for anonymous class?

public class Class1 {

protected InnerClass1 ic;

public Class1() {

ic = new InnerClass1();

}

public void displayStrings() {

System.out.println(ic.getString() + ".");

System.out.println(ic.getAnotherString() + ".");

}

static public void main(String[] args) {

Class1 c1 = new Class1();

c1.displayStrings();

}

protected class InnerClass1 {

public String getString() {

return "InnerClass1: getString invoked";

}

public String getAnotherString() {

return "InnerClass1: getAnotherString invoked";

}

}

}

we can do like . crieate inner class instanve in side of main class defalut consiter . use the object we do the our bz logic

WAP to initilize a character and print whether it is a vowel or not?

// short version

char c = 'a';

System.out.println((c=='a'c=='e'c=='i'c=='o'c=='u')?"VOWEL":"CONSONANT");

// easy to read version

char c = 'a';

switch (c) {

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

System.out.println("VOWEL");

break;

default:

System.out.println("CONSONANT");

}

What is the difference between classes and objects?

Class

Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors). Classes provide modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of the class should make sense in context. Also, the code for a class should be relatively self-contained (generally using encapsulation). Collectively, the properties and methods defined by a class are called members.

Object

A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.
A class is the type definition of an object. An object is an actual instance of a class type.

What is the difference between mutuator and accessor methods?

Mutator: Put something into the class (change something). By convention, has a name that starts with "set", for example, "getName".

Accessor: Get something out of the class (find out the class's state). By convention, has a name that starts with "get", for example, "getName". For logical values, "is" is used instead, for example, "isActive".

Java programming final test?

Can you answer in this java programming quiz? just 1 or 2 answers are fine..

1. The != operator is used to evaluate _______ between two values.

Non-equality

!!

=!

Equality

2. The interface part of a class is created using

package access

protected visibility

private visibility

3. User defined data type or objects (like a Car class object) are passed by

Reference

Value

Copy

Placement

5 . If you declare constructors private, they ________ be called by all classes and methods to create objects.

Can

can privately

can virtually

Can t

6. Components are placed in a container by calling the __________ method.

registerComponent ()

addActionListener ()

setLayout ()

add ()

7. If an abstract method is not defined in a subclass, the subclass is

Abstract

Overridden

Promoted to the superclass

Hidden

8. Subclass objects of different type that inherit from the same superclass

Can be treated as superclass objects

Can't share private superclass data

Don't share the same superclass data

Don't share any data

9. Polymorphism

Promotes and allows sharing of data and methods between classes

Allows development of general solutions and the runtime selection of methods

Prevents methods outside a class from directly accessing instance variables

Allows classes to contain/combine both data (properties) and methods (behaviors)

10. Inheritance relationships form _________.

Hierarchical Structures

Peer to Peer Networks

Loosely bounded sets

Network Structures

11. Your program class that inherits from JPanel ____________.

Uses a constructor to initialize objects

Inherits the behavior of a JObject program

Is unable to override paint_component ()

Knows nothing of drawing

12. The paint_component method ___________.

Must be private

Expects a Graphics object argument

Never calls super.paint_component (g)

Must be called by you

13. Successful use of polymorphism depends on

Identifying generic methods

Identifying an inheritance class composition

Identifying abstract objects

Deciding on how to use subclass references

14. A superclass method can be overridden in a subclass in order to

Change the name

Make the method private

Provide new behavior appropriate to the subclass

Allow hidden access

16 . In order to place subclass objects of different type in an array

They must inherit from the same subclass

The array reference itself must be of superclass type

You must cast and add-in missing superclass members

They must be of the same type

17. The name of the event handler method of the ActionListener interface is ____.

itemStateChanged

mouseMoved

actionPerformed +

keyPressed

18. Downcasting

Converts a subclass reference to a superclass reference

Converts a subclass object to a superclass object

Converts a superclass reference to a subclass reference

Converts a superclass object to a subclass object

19. A simple layout manager is the ____ manager.

GridLayout

BoxLayout

FlowLayout

PanelLayout

20. An individual array element is accessed using its _________.

Type

Subscript/Index

Value

Object

21. Since it generates byte codes, the Java compiler is _________.

An interpreter

Not a true compiler

Object code

Windows instructions

22. When GUI components are displayed on the screen, a/an ___________ positions and displays those components.

Layout manager

Container object

Event handler

Interface class

23. An executing program class must contain a/an

Body

Main method

Instance variable

Comment

24. Referring to a superclass object through a subclass reference

Can cause a runtime error

Requires that the superclass reference refers to the same subclass object as the cast

Requires an explicit object to avoid the compilation error

25. Polymorphism works for

Unrelated classes

Overloaded classes

An inheritance hierarchy of classes

Has-a classes

26. Given that a=4, b=10, c=2, d=5, and x=3, the expression "(a + c) / c * d + a" evaluates to

20

4

128

19+

27. When dynamic binding takes place, the Java system

Determines the type of the object at compile time

Binds a method name to its address at runtime

Moves up the inheritance hierarchy

Makes a list of generic event handlers

28. When dynamic binding takes place, the Java system

Determines the type of the object at compile time

Binds a method name to its address at runtime

Moves up the inheritance hierarchy

Makes a list of generic event handlers

29. If a subclass constructor explicitly calls a superclass constructor

It overrides the superclass constructor

It must call the noarg constructor

It cannot access protected data

The call must be the first line of code

30. The composition relationship can be identified as a/an _________ relationship.

Enduring

Has/a

Is/a

Overridden

31. When declared with a static lifetime,

Variables exist during the execution of a method

Variables exist from beginning to end of program execution

Methods and instance variables are visible

Variables declared outside the {} of a block

32. When an array is passed as an argument to a method, _____.

The array is passed by reference

Only the first value is passed

The called method can't change array values

It is passed by value

33. When the expression "result = total + input * 2;" is evaluated the __________ is placed in the location of the left hand side.

Left side result

Assignment

Right side result

Operator

34. When invoked, a subclass constructor

Automatically calls the noarg constructor of the superclass (if one is not explicitly called)

Cannot access superclass data even if protected

Returns a superclass object

Does not use the superclass constructor

35. When declared with automatic scope,

Variables exist during the execution of a method

Variables exist from beginning to end of program execution

Methods and instance variables are visible

Variables declared outside the {} of a block

36. The inheritance class relationship

Promotes and allows sharing of data and methods between classes

Allows development of general solutions and the runtime selection of methods

Prevents methods outside a class from directly accessing instance variables

Allows classes to contain/combine both data (properties) and methods (behaviors)

37. When a subclass method has the same name and type as a superclass method but accepts different arguments, that's an example of

Protected inheritance

Overriding

Polymorphism

Overloading

38. When you want to declare constant values, you would use the ________ keyword.

objects

final

const

Static variables

39. The toString method

Is not provided by class Object

Is called implicitly to convert an object to a String

Can be overloaded

Can't be overridden

40. A switch statement tests a variable or expression against __________.

Constant, integral values

Expressions

Decimal numbers

Strings

41. Reffering to a superclass object through a subclass reference

can cause a runtime error

Requires that the superclass reference refers to the same subclass object

Requires an explicit object to avoid the compilation error

48. Because all variables must be declared before their use, Java

Is an object oriented language

Is a strong variable language

Is a strongly typed language

Is a typing language

49. Variables declared/defined inside a method are

General variables

Visible everywhere

Local variables

Instance variables

50. The while statement

Is the most specific form of repetition

Requires initialization, test, and change of a control variable

Will never skip execution of the loop body

Doesn't use a control variable

51. The do-while statement

Does not require a test of cond

Is the most specific form of repetition

Executes at least once

Is intended to replace for statements

52. The use of superclass references to refer to subclass objects has been found useful

For container objects

When placing elements of subclass objects in a superclass array

When placing elements of superclass objects in a subclass array

For Composition classes

53. Overloaded constructors are used to

Create different types of objects

Create two dimensional arrays

Create primitive data types

Initialize objects of the same class in different ways

54. Java's mechanism for placing related classes in a subdirectory uses the ________ keyword.

package

import

extensible

extends

55. this

is a reference to an object

Can t be used to access object data

Is passed to every class

Is hidden and private

56. An anonymous inner class ____.

Has a class name

Typically is used to define an event handler class

Has no scope

Contains anonymous methods

57. The new operator is used to __________.

Allocate memory for an object or array

Call an object destructor

Call the garbage collector

Promote visibility

58. The drawOval method takes the same four arguments as ________.

drawLine ()

drawPoly ()

drawRect ()

fillArc ()

59. In order for a class method to access the private data of a class object (not of its class)

The data can only be read

It can't

The data can be changed directly

get/set methods must be used

Question 62

62. Class methods ________ access all class variables, public or private.

Can

Can't

64. A reference in a Java program

Causes object initialization

Refers or points to a runtime data type

Causes memory allocation

Refers (or points to) to an object

65. The listener interface for radio buttons is the ____ interface.

ActionListener

MouseListener

KeyListener

ItemListener

66. implementation part of a class is created using

67. What types of superclass methods/variables can be directly accessed from a sub class?

public

private

provoked

68. The statement(s) in the body of an if structure should be indented in order to

Compile properly

They shouldn t

Stand out and enhance readability

Allow repetition

69. The sdk program that is the JVM and runs programs is

java

javac

appletviewer

Write a program to encrypt a string and decrypt it using your own method of encryption and decryption.?

We'll use a simple method of encryption: xor encryption

// same function used to both encrypt and decrypt

void crypt(char *str) {

const int key = 0x86;

// crypt each individual character of str

int i;

const int length = strlen(str);

for(i = 0; i < strlen(str); ++i) {

str[i] = str[i] ^ key;

}

}

IS Throwing an exception always causes program termination?

No Not necessarily..

You can catch an exception and then continue to execute your program as if nothing happened.

If in your catch block you have code like System.exit then your program would terminate.

What is unicast remote object in java?

The UnicastRemoteObject class defines a non-replicated remote object whose references are valid only while the server process is alive. The UnicastRemoteObject class provides support for point-to-point active object references (invocations, parameters, and results) using TCP streams.

What is the defference between bisection method and newton method?

there are three variable are to find

but in newton

only one variable is taken at a time of a single iteration

A Java method that takes an integer array as a formal parameter and returns the sum of integers contained within the array?

I don't know if I fully understand but here goes

int a[]={2,4,8,0,2};

int y = 0;

int x = 0;

while(y<a.length()){

x += a[y];

y++;

}

then you just reference x.

How could Java classes direct program messages to the system console but error messages say to a file?

Sytem.out amd System.err is used to get the output, and refer to standard output strem and which is by default console ... u can send the output to any other stream or log file either by importing java.io.file and show the output to the console and run time error to any file using exception handling .... or u can give the command while running the program which will redirect this to log file as ... java ClassName 2error.log this will send error message to error.log file and output to console. ... u can send both the output and error to log file as... java ClassName output.log 2 error.log ... here ClassName is the name of the calss file which u wanted to execute

If you enter same function name into two interfaces than how you implement these two into one class?

If you have two (or more) interfaces with the same method definition like this:

public interface one(){

public int doSomething();

}

public int interface two(){

public doSomething();

}

You can implement both in the same class:

public class myClass implements one, two{

public int doSomething(){

// the code to do something goes here

return 0;

}

}

And you only have to write the implimentation once. This code is valid for all the interfaces implemented with a doSomething() method.

What are the methods of hash table?

See the related link below for the Java API documentation for the Hashtable class and its methods.

What r conditional loops and unconditional loops in c?

A conditional loop will only continue to loop while the given condition is true:

while( i < 10 ) {

...

}

An unconditional loop either has no condition (a GOTO loop), or the condition is guaranteed to always evaluate to true:

while( true ) {

...

}

Write an equivalence of switch statement and if statement?

switch(ch)

{

case '+':

....cout <<"arithmetic operator";

....break; //<===break is a must

/// <====================other cases

.

.

default: // <=======else

}

Does a collection require an array?

No. While there are quite a few which use arrays to store their data (ArrayList, HashMap, Vector, etc.) the typical counter example is a LinkedList. Java's implementation of the LinkedList class uses the standard Entry-Entry.next method of connecting elements in the list.

You can even consider a collection as something that is similar to an array but with enhanced features. Collections have a lot of features that arrays do not have.

What is meaning of word persistence in java?

Persistence is the term used to refer to the situation where the objects data is stored even after the object is destroyed or the application is closed.

Persistence is implemented using Serialization where the data of the object is serialized into flat files and stored into the system. These files can be de-serialized to form the objects by using de-serialization.