#include<iostream>
#include<time.h>
// return a reference to the largest of two objects of the same type
template<typename T>T& largest (T& a, T& b) {
return a<b?b:a;
}
int main() {
// seed the random number generator
srand ((unsigned) time (0));
// repeat 10 times
for (size_t loop=0; loop<10; ++loop)
{
// select two random integers
int x = rand()%10;
int y = rand()%10;
// print the result
if (x==y)
cout << x << " & " << y << " are equal" << endl;
else
cout << "The largest of " << x << " & " << y << " is " << largest (x,y) << endl;
} // end for loop
}
Explain all the control statements in java?
The if and switch statements are commonly referred to as decision statements. When we use decision statements in our program, we're asking the program to evaluate a given expression to determine which course of action to take. It decides the control flow of the program.
Write a Java program which create variable size array in Java?
import java.util.Scanner; class Addition
{
public void Addition()
{
Scanner s = new Scanner(System.in); int a[][] = new int[3][]; int b[][] = new int[3][]; int c[][] = new int[3][]; for(int i=0;i<3;i++)
{
a[i] = new int[3]; b[i] = new int[3];
c[i] = new int[3];
}
System.out.println("Enter the elements of var size array a"); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the elements of var size array b"); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j] = s.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
System.out.println("The resultant matrix is: "); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
public class AdditionTest
{
public static void main(String args[])
{
Addition a = new Addition();
a.Addition();
}
}
Where you have to type a java program?
Notepad, or any other text editor. If you have the compiler installed, you can run the programs on the Command Prompt. I recommend blueJ because it does not require a compiler to already be downloaded though.
Who created c and c plus plus and java?
Dennis Ritche developed C for bell labs for the unix operating system http://en.wikipedia.org/wiki/C_(programming_language) Bjarne Stroustrup developed C++ http://en.wikipedia.org/wiki/C%2B%2B
Why in java string args is passed in main method?
The String[] args parameter is an array of Strings passed as parameters when you are running your application through command line in the OS. The java -jar command will pass your Strings update and notify to your public static void main() method.
To learn more about data science please visit- Learnbay.co
Why run time polymorphism is dynamic and compile time polymorphism is static?
The simple answer is that compile-time polymorphism occurs at compile time while runtime polymorphism occurs at runtime.
The actual answer is that compile-time polymorphism results in the compiler generating source code on your behalf while runtime polymorphism relies on function pointers or virtual methods to determine the next instruction at runtime.
Compile-time polymorphism therefore applies to template functions and classes since that is the only way the compiler can generate source code on your behalf. To achieve this, the runtime type for the template parameters must be fully-defined at compile time, even if those types have runtime polymorphic characteristics of their own.
Runtime polymorphism applies to virtual methods and function pointers, both of which can be used to dynamically alter the execution path of your program. Virtual methods are made possible through virtual tables, which are essentially just arrays of function pointers. Each runtime type that derives from a base class with virtual methods provides its own virtual table, thus the runtime type determines which specific function overrides will be invoked at runtime, even if the runtime type cannot be determined at compile time. In this way you can generalise your code to work with the base type but still get the expected polymorphic behaviour whenever a derived type is passed instead.
For one: you cannot write applets (web-browser-extensions) in C.
Can you declare more than one single main method in java program why?
It doesn't really make sense. The JVM needs to know where to start running the program; that's what the "main" method is for. A class - one that can be run directly - needs to have a single entry point, to avoid ambiguity. This main method can then call any number of other methods.
I assume you can call a second method "main", but using a different signature. However, to avoid confusion, I would recommend you don't do this.
Why are operating systems written in the C language?
Speed, power, efficiency, well supported libraries, generally. Almost everything (But not absolutely everything.) has at least C bindings one can use.
Not to mention you're guaranteed to have the standard C library to handle a great deal of the core functionality of the program.
What is the difference between run time and compile time in c?
Compile time errors are detected at the compilation of a program. These are usually syntactical errors. A program cannot be compiled and hence executed if it contains a compile time error.
for example,
int a b=0;
above statement will produce a compile time error as a and b must be separated by a comma(,).
Runtime errors are logical errors. They don't affect the compilation of the program but causes a program to produce unwanted output. The program becomes buggy.
for example,
sum=sum-a[i];
in above statement, no syntactical error is present and hence the program will get executed successfully. But from a logical point of view, sum was supposed to be the addition of array elements. using minus(-) instead of plus(+) will give unwanted or incorrect output.
Definite interations: Repeated sequence of calculations/ operations for a fixed number of repetitions. (Do or For loop) Indefinite iteration: no set limit on iterations, generally "while" loops. multiple interations is the same as multiple repetitions or trials.
How do you write an algorithm for list of names in ascending order?
If they're in an array, use java.util.Arrays.sort(). If they're in a Collection, as in java.util.Collection, use java.util.Collections.sort().
If you want an actual algorithm, try a for-each loop with an equivalence operator and conditional switch.
Yes - that's the standard program used to run programs written for the Java technology. Just be sure you get the real Java download, and not some fake - in other words, download from the official Java site, or from a reputable site such as tucows, download.com, etc.
What are static objects in java?
There is no separate entity as a static object in java. The static keyword in java is used to signify that the member (either a variable or a method) is not associated to an object instance of the class. It signifies the fact that the member belongs to the class as a whole. The words static and objects are opposites of one another so you cannot have a static object.
However, you can declare an object as a class level variable which could be referred to as a static object but it will be referred to as a static or class variable and not a static object.
An instance of a class is also known as an object. In the Java language, you use the new operator. Here is an example:
Integer x; // Using the wrapper class, Integer. This doesn't create the object yet.
x = new Integer(5); // This will create the instance.
These commands can be combined into one:
Integer x = new Integer(5);
What is inheritance in JAVA programming?
Inheritance in JAVA programming is the process by which one class takes the property of another class.
The new classes, known as derived classes or subclasses, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or super classes.
It helps the programmer to re-use the class just by modifying minor objects and methods in it.
class person
{
attribute-name,address
}
class Emp extends person
{
attribute-(same as parent class)name,address
own attribute-salary(modification)
}
Ascending order program for java?
public class BubbleSortAscendingOrderDemo
{
public static void main(String a[])
{
//Numbers which need to be sorted
int numbers[] = {23,5,23,1,7,12,3,34,0};
//Displaying the numbers before sorting
System.out.print("Before sorting, numbers are ");
for(int i = 0; i < numbers.length; i++)
{
System.out.print(numbers[i]+" ");
}
System.out.println();
//Sorting in ascending order using bubble sort
bubbleSortInAscendingOrder(numbers);
//Displaying the numbers after sorting
System.out.print("Before sorting, numbers are ");
for(int i = 0; i < numbers.length; i++)
{
System.out.print(numbers[i]+" ");
}
}
What is the difference between abstract class and normal class?
Any class which has one or more abstract methods is called an abstract class. But in the normal class we can't have any abstract methods.
We cannot create an object for the abstract classes.
When we inherit the abstract class we should implement the abstract method which we inherit.
Every complete statement ends with a?
Any teacher will expect you to answer by saying a semicolon (;), but this is not strictly true. First of all, the definition of a "line of code" varies from teacher to teacher and textbook to textbook. Second, even the Java Language Specification lists several types of Java statements which do not need to end in a semicolon.
In general, a complete Java statement will end in either of semicolon or a closing block brace.
How do you get a java compatible web browser?
Most browsers nowaday are compatible with Java. In some cases you need to install Java as a plug-in. In this case, the browser will usually ask you automatically, whether you want to install Java, as soon as it is needed - i.e., as soon as you access a Web page that requires Java support.
There is no such thing as an access specifier in Java. There are access modifiers.
An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:
1. Public
2. Protected
3. Default and
4. Private
Private is the most restrictive access modifier whereas public is the least restrictive. The default access modifier if unspecified is to allow access to classes in the current package only, except within an interface where the default is 'public'.
How many types are there in java?
The two basic data types in Java are primitives and objects.
Primitives: byte, char, short, int, long, float, double, boolean
Objects: Everything else.
Java is renouned as one of the most classic OOP engines. OOP or object oriented programming reduces all functions to various tasks of objects. A heirarcy of classes of objects all inherit the knowledge and aptitude of class they "extend". Eventually, every object can be traced back to the class "Object". Java is known as a very common language, with a structure that can be a good starting point to jump to almost any language.