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

What a java program for arithmetic operations between two integers?

public static void main(String[] args) {

int a = 5;

int b = 10;

int c;

c = a + b; // addition

c = a - b; // subtraction

c = a * b; // multiplication

c = a / b; // division

}

How ploymorphism and inheritance is different from that in Java and c plus plus?

C++ allows multiple inheritance while Java does not. In my opinion, multiple inheritance is not useful because it can get very confusing very quick.

For polymorphism, C++ does early binding by default, while Java does late binding by default. Late binding is more useful than early binding.

Is it possible to use arrays when using the java programming language?

It is possible to use arrays when employing java programming language. There are many different series of programming choice that can be employed with various end results.

Why is it important to have a solid background in mathematics for programming languages?

You need to to know some basic math to live. right? Likewise you need to know some basic math to start computer programming....beginners start out by learning to do programs like solving a quadratic equation....if you are not thorough with algebra, u can't do it. The way you think is more important.....rather than being good in math, computer programmers are required to think the way math people think - vey very very logically.

Which type of inheritance does not support by java?

Desoola Anil Kumar, Java Faculty: There are 5 types of Inheritance. 1.Single inheritance, 2.Multiple inheritance, 3.Multilevel Inheritance, 4. Hirarichal inheritance and 5. Hybrid inheritance. Java supports multi level inheritance and hirarichal inheritance.

When do you say an exception is handled?

There is no catch block that names either the class of exception that has been thrown or a class of exception that is a parent class of the one that has been thrown, then the exception is considered to be unhandled, in such condition the execution leaves the method directly as if no try has been executed

C program to read a text and count occurrence of a particular word?

#include<stdio.h>

main()

{

int i,words,spaces;

char a[30];

printf("enter the string") ;

scanf("%s",a);

for(i=0;a[0]!="\0";i++)

{

if (a[i]=' ')

spaces++;

}

printf("the no. of spaces in the string is %d",spaces);

printf("the number of words in the string is %d",spaces+1);

}

Program for dequeue in data structure?

Display function in de queue

void display()

{

int i;

if((front == -1) (front==rear+1))

printf("\n\nQueue is empty.\n");

else

{

printf("\n\n");

for(i=front; i<=rear; i++)

printf("\t%d",queue[i]);

}

}

Write a test program that prints a 3 by 3 matrix?

4.(Displaying matrix of 0s and 1s ) write a method that displays by n by n matrix using the following header : Public static void printMatrix(int n) Each element is o or 1,which is generated randomely. Write a test program that prints a 3 by 3 matrix that may look like this: 010 000 111 4.(Displaying matrix of 0s and 1s ) write a method that displays by n by n matrix using the following header : Public static void printMatrix(int n) Each element is o or 1,which is generated randomely. Write a test program that prints a 3 by 3 matrix that may look like this: 010 000 111

Explain the meaning of abstract keyword in relation to classes and methods?

The abstract keyword is used to denote abstract classes and abstract methods in Java.

An abstract method is a method which is defined, but not implemented:

public abstract void doStuff();

An abstract class is a class which contains one or more abstract methods*, and which cannot be instantiated directly. All subclasses of an abstract class must implement the abstract methods.

* Note that abstract classes can include no abstract methods, but this rather defeats the purpose of using an abstract class.

The quintessential example of an abstract class is the Shape class.

// Definition of our abstract class

public abstract class Shape {

// Notice how we can actually declare and implement variables and methods.

// This is what differentiates between an abstract class and an interface.

// The location of this shape

private double x,y;

// Change our location - all subclasses will have this by default

public void moveTo(final double newX, final double newY) {

x = newX;

y = newY;

}

// Definitions of our abstract classes.

// All classes which extend from Shape must implement these.

public abstract double getArea();

public abstract double getPerimiter();

}

// Definition of our concrete example class

public class Rectangle extends Shape {

// Beyond the x,y location of Shape, Rectangle must have width and height

private double width, height;

// Implement abstract methods

public double getArea() {

return width * height;

}

public double getPerimiter() {

return width + width + height + height;

}

}

Can you have a final abstract class?

No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.

Combining them would lead to an unusable class, so the compiler will not let this happen.

Give sample program?

class firstprog

{

public static void main (String args[])

{

System.out.pritnln("My first Program in Java ");

}

}

Simple Java Programe

Write the above program in Ms dos's edit or Notepad of Windows save it with "firstprog.java" run the commands given below in command prompt of Ms Dos to get the output Remamber Java is case sensative so mentain capital and small letter.

1. javac firstprog.java

2. java firstprog

What is an abstract interface?

An abstract interface is an interface which has a one-to-many relation - providing multiple paths from a single location to multiple locations.

Is the java programming language a complete language or is it useful only writing programs for the web?

You can also write desktop programs. You can use Java for almost anything.

You can also write desktop programs. You can use Java for almost anything.

You can also write desktop programs. You can use Java for almost anything.

You can also write desktop programs. You can use Java for almost anything.

What is relationship between string and char data type?

The char type store only one symbol of data (letter, digit, space, tab and so on). The string type is able to keep virtually data of any length. Also string type is based on arrays and uses reference type working with memory.

How java runs in command prompt?

The Java executable comes with a showversion flag, so typing:

java -showversion

Will display something like:

java version "1.6.0_16"

Java(TM) SE Runtime Environment (build 1.6.0_16-b01)

Java HotSpot(TM) 64-Bit Server VM (build 14.2-b01, mixed mode)

Note: This seems to be bugged on my machine. After printing out the above information, it also prints out the rest of the command line switches. If this happens to you, you may have to scroll up to see the important information.

Write a program that accepts a word in uppercase and prints it in lowercase?

There are two functions in ctype.h: toupper() and tolower(). First one to make character into upper case and the second one for making lower case.

Here is a small program converting inputted character into lower case:

#include

#include

int main() {

char ch;

printf("Enter the char: ");

scanf("%c", &ch);

printf("Char in lowercase: %c\n", tolower(ch));

return 0;

}

Testing:

Enter the char: G

Char in lowercase: g

Enter the char: S

Char in lowercase: s

What operator can be used to compare 2 values in java?

One possible way (although much less efficient than using the operators directly) is by using BigInteger:

int a = 5, b = 7;

int sum = BigInteger.valueOf(a).add(BigInteger.valueOf(b)).intValue();

However, BigInteger.add() might use arithmetic operators in its own calculations; they are simply hidden from the programmer's view.

How are java threads created?

Thread can be created in two ways either by extending Thread or implementing runnable interface

Examples:

public class A extends Thread { } public class A implements Runnable { } By using both the methods we can create a Java Thread. The disadvantage of extending the Thread class is that, you cannot extend the features of any other class. If we extend the Thread class we cannot extend any other class whose features we may require in this class.

So it is usually advisable to implement the Runnable Interface so that we can extend the required features and at the same time use the Thread functionality.

How many classes are there in java?

there is one method only. go to command prompt and type this to know the methods.

D:\java>javap java.awt.event.ActionListener

Compiled from "ActionListener.java"

public interface java.awt.event.ActionListener extends java.util.EventListener{

public abstract void actionPerformed(java.awt.event.ActionEvent);

}

What are Class Constructor and Primitive data types?

class constructor is a function which has the same name as the class name and has no return type.

primitive data types are the fundamental data types which are independent.

eg:int,char,float etc..............

How do you write a Program in java to cheak a number is automorphic number?

Source Code ::

import java.io.*;

class automorphic

{

protected static void main()throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number: ");

int a=Integer.parseInt(in.readLine()),b=a,c=0,e=a*a;

while(b>0)

{

c++;

b/=10;

}

double d=Math.pow(10,c-1);

if(a==e%d)

System.out.println("Automorphic number!!");

else

System.out.println("Not an Automorphic number!!");

}}

How do you code to inherit from an interface in java?

We have been using references to the term "Implementing an Interface" throughout the preceding chapters and we havent yet actually dug deep into this topic. As you might remember, an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior.

When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not)

Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class.

Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile:

public class Ball implements Convertible { // Keyword 'implements'

public void openHood() { }

public void setOpenHoodFactor(int bf) { }

}

Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all…

Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following:

• Provide concrete (nonabstract) implementations for all methods from the declared interface.

• Follow all the rules for legal overrides.

• Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method.

• Maintain the signature of the interface method, and maintain the same return type (or a subtype).

• It does not have to declare the exceptions declared in the interface method declaration.

Why is quick sort and merge sort called stable algorithms?

Quick sort is not stable, but stable versions do exist. This comes at a cost in performance, however.

A stable sort maintains the order of equal elements. That is, equal elements remain in the same order they were input. An unstable sort may change the order. In some cases, the order of equal elements is of no consequence, but when two elements with different values have the same sort key, then order can be important.