Found 33676 Articles for Programming

Examples of soft references and phantom references?

AmitDiwan
Updated on 17-Aug-2020 09:17:59

281 Views

Soft references are often used to implement memory-sensitive caches. Let us see an example of soft references in Java −Example Live Demoimport java.lang.ref.SoftReference; class Demo{    public void display_msg(){       System.out.println("Hello there");    } } public class Demo_example{    public static void main(String[] args){       Demo my_instance = new Demo();       my_instance.display_msg();       SoftReference my_softref = new SoftReference(my_instance);       my_instance = null;       my_instance = my_softref.get();       my_instance.display_msg();    } }OutputHello there Hello thereA class named Demo contains a function named ‘display_msg’, that displays the relevant ... Read More

Maximum number of dots after throwing a dice N times in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:07:36

164 Views

Given the task is to calculate the maximum number of dots that can be expected after throwing a dice N times having M faces.The first face of the dice contains 1 dot, the second face has 2 dots and so on. Likewise the M-th face contains M number of dots.The probability of appearance of each face becomes 1/M.Let’s now understand what we have to do using an example −Input − M=2, N=3Output − 1.875Explanation − The dice has 2 sides = {1, 2}If the dice is thrown 3 times then the sample space will be = MN = 23{(1, 1, ... Read More

Unreachable statement using the non-final variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:06:35

130 Views

Following is an example, wherein we will see unreachable statement using the non-final variable −Exampleclass Demo_example {    int a = 2, b = 3;    void display_msg(){       while (a < b){          System.out.println("The first variable is greater than the second");       }       System.out.println("This is an unreachable statement");    } } public class Demo{    public static void main(String args[]){       Demo_example my_instance = new Demo_example();       my_instance.display_msg();    } }Output“The first variable is greater than the second” displayed infinitelyA class named Demo_example, that defines ... Read More

Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n in C++

sudhir sharma
Updated on 17-Aug-2020 09:17:02

1K+ Views

In this problem, we are given a number n which defines the n-th term of the series 2^0, 2^1, 2^2, …, 2^n. Our task is to create a program to find the sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n.Let’s take an example to understand the problem, Inputn=6Output Explanation sum = 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 sum = 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127A simple solution to the problem is by using the loop. Finding the 2^i, for each value from 0 ... Read More

Maximize number of continuous Automorphic numbers in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:05:49

307 Views

Given the task is to maximize the number of continuous Automorphic elements in a given array with N number of elements.An automorphic number is a number whose square ends with the same digits as the number itself. For example 5 is an automorphic number as 5*5 = 25 and 25 ends with 5.Let’s now understand what we have to do using an example −Input − arr[]={5, 3, 625, 6, 8, 1}Output − 2Explanation − Automorphic numbers present in the above array are 5, 625, 6 and 1 but the maximum continuous automorphic numbers are {625, 6} which makes the output ... Read More

Unreachable statement using final variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:04:48

202 Views

Unreachable statements are those that don’t get executed when the code is being executed. This could be so because −There is a return statement before the code.There is an infinite loop in the code.The execution of the code is terminated forcibly before it executes.Here, we will see how the unreachable statement can be used with the ‘final’ keyword −Example Live Democlass Demo_example{    final int a = 56, b = 99;    void func_sample(){       while (a < b){          System.out.println("The first value is less than the second.");       }       System.out.println("This ... Read More

Types of References in Java

AmitDiwan
Updated on 17-Aug-2020 09:02:34

840 Views

There are four different kinds of references based on the way in which the data is garbage collected.Strong referencesWeak referencesSoft referencesPhantom referencesStrong referenceIt is the default type of reference object. An object that has active strong reference can’t be garbage collected. It is possible only if the variable that is strongly referenced points to null. Let us see an example −Exampleclass Demo {    //Some functionality } public class Demo_example{    public static void main(String[] args){       Demo my_inst = new Demo();       my_inst = null;    } }Weak referenceThey are not default class of reference ... Read More

Maximize the size of array by deleting exactly k sub-arrays to make array prime in C++

Sunidhi Bansal
Updated on 17-Aug-2020 09:03:00

138 Views

Given the task is to delete exactly K sub-arrays from a given array Arr[] with N positive elements such that all the remaining elements in the array are prime and the size of the remaining array is maximum.Input Arr[]={4, 3, 3, 4, 3, 4, 3} , K=2Output 3Explanation − K=2, this means only 2 sub-arrays must be deleted.The sub-arrays deleted are Arr[0] and Arr[3…5] which leaves the array Arr[]={3, 3, 3} with all the prime elements and the maximum size possible.Input Arr[]={7, 6, 2, 11, 8, 3, 12}, K=2Output 3Explanation − The sub-arrays deleted are Arr[1] and Arr[4…6] and the remaining prime array is ... Read More

Maximize the sum of products of the degrees between any two vertices of the tree in C++

Sunidhi Bansal
Updated on 17-Aug-2020 08:59:08

294 Views

Given the task is to construct a tree with a given integer N such that, the sum of degree(x) * degree(y) for all ordered pairs (x, y) is maximum and x is not equal to y.Input −N=5Output −50Explanation    1     \      2       \        3          \           4             \              5 Degree of 1st node = 1 Degree of 2nd node = 2 Degree of 3rd node = 2 Degree of 4th node ... Read More

Maximum distinct lines passing through a single point in C

Sunidhi Bansal
Updated on 17-Aug-2020 08:55:56

233 Views

We are given the number N and coordinates of two points (x1, y1) and (x2, y2) for each line. The goal is to find the maximum number of lines from given lines that can pass through a single point such that no two lines cover each other, and no rotation is performed.We will represent lines as pair of (m, c) where y=mx+c and m is slope m=y2-y1/x2-x1Lines with same m are parallel given c1!=c2. We will count distinct slopes(m). For vertical lines if x1=x2, slope = INT_MAX else m.Let us understand with an example.Input Line 1 (x1, y1)=(4, 10) (x2, y2)=(2, ... Read More

Advertisements